Taxonomy Service for PHP
Use the AppSpice online interface to manage complex record categorization, tagging and flagging. Then copy/push the resulting schemas to your local/production environments for entity taxonomy management.
This is the vanilla PHP version of the taxonomy schema object processor.
There's a fully functional example in the package. It includes a taxonomy.json file and a working SQLite database. (The SQLite mapping tables will be sufficient for many projects.)
The example project also includes useful HTML scripts for producing dynamic category and tagging options.
Installation
$ composer require app-spice/taxonomy-php
Or download the package from GitHub.
Data Management
Create entity tables as usual for your project. Never store taxonomy relationships in your entity tables. Use mapping tables instead. One for your entity categories, and one for your entity tags. Use joins to perform queries.
A single set of generic mapping tables would keep things simple. The caveat being -- There's no foreign key relationships, so you'll have to perform deletes when an entity is deleted.
create table entity_category
(
entity_id varchar(20) not null,
category_id varchar(20) not null,
primary key (entity_id, category_id)
);
create table entity_tags
(
entity_id varchar(20) not null,
tag_id varchar(20) not null,
primary key (entity_id, tag_id)
);
You could create a set of mapping tables for each entity. This would result in many additional tables added to your project, but you would have the luxury of foreign key relationships.
Usage
HTML
List management.
$taxonomyClient = new TaxonomyClient('my-taxonomy-schema.json');
// Bootstrap 4/5 select.
echo '<div class="btn-group">' . $taxonomyClient->selectFilter('c', 't', 'custom-select form-select') . '</div>';
// Produce table from request...
Data Collections
Categorized entities.
select e.id, e.title
from my_entiies e
join entity_category c on e.id = c.entity_id
where c.category_id = 'cat_uj478jfdjty5hgfh'
order by e.title;
Categorized and tagged entities.
select e.id, e.title
from my_entiies e
join entity_category c on c.entity_id = e.id
join entity_tags t on t.entity_id = e.id
where c.category_id = 'cat_mi58mnww47t822jd'
and t.tag_id = 'tag_lrjwvol85gnobo3u'
order by e.title
Data Management
Updating an entity category.
update entity_category set category_id = 'cat_299t4552qasnxcyt' where entity_id = 'ent_uj478jfdjty5hgfh';
Updating entity tags.
delete from entity_tags where entity_id = 'ent_uj478jfdjty5hgfh';
-- You'd determine tags dynamically and perform each insert in a loop
insert into entity_tags (entity_id, tag_id) values ('ent_uj478jfdjty5hgfh', 'tag_lrjwvol85gnobo3u');
-- ...
Deleting an entity.
delete from entity_category where entity_id = 'ent_uj478jfdjty5hgfh';
delete from entity_tags where entity_id = 'ent_uj478jfdjty5hgfh';