dantleech/phpcr-taxonomy-bundle
Installation:
composer require dantleech/phpcr-taxonomy-bundle
Add to your AppKernel.php (or bundles.php for Symfony 5+):
new DTL\PhpcrTaxonomyBundle\PhpcrTaxonomyBundle(),
Configure Taxonomy Paths:
Define paths in config/packages/phpcr_taxonomy.yaml:
dantleech_phpcr_taxonomy:
taxonomies:
tags: /var/www/data/tags
categories: /var/www/data/categories
Annotate Your Document:
Add @Taxons (string array) and @TaxonObjects (collection) to your PHPCR document:
use DTL\PhpcrTaxonomyBundle\Metadata\Annotations as Taxonomy;
/**
* @Taxonomy\Taxons()
* @Taxonomy\TaxonObjects()
*/
class Article
{
// ...
}
First Use Case: Tag a document via repository:
$article = new Article();
$article->setTaxons(['laravel', 'phpcr']);
$dm->persist($article);
$dm->flush();
Tag Assignment:
Use setTaxons() to assign tags (creates missing taxons automatically):
$post->setTaxons(['symfony', 'phpcr', 'taxonomy']);
Bulk Operations:
Use the TaxonManager service to manage taxonomies:
$taxonManager = $container->get('dantleech_phpcr_taxonomy.manager.taxon');
$taxonManager->getTaxon('/var/www/data/tags/laravel');
Referrer Counts: Initialize counts via CLI:
php bin/console dantleech:phpcr-taxonomy:fix-referrer-counts
Querying by Taxon:
Use PHPCR queries with TaxonObjects:
$qb = $dm->createQueryBuilder('Article');
$qb->where($qb->expr()->in('taxonObjects', $taxon));
TaxonEvents:
$dispatcher->addListener(TaxonEvents::POST_CREATE, function ($taxon) {
// Custom logic on taxon creation
});
Taxon to add fields:
class CustomTaxon extends Taxon
{
/**
* @PHPCR\String()
*/
private $description;
}
@Taxons:
$builder->add('taxons', CollectionType::class, [
'entry_type' => TextType::class,
'constraints' => [new Length(['max' => 50])],
]);
Path Configuration:
Command:
$dm->createDocument('/var/www/data/tags')->save();
Referrer Counts:
fix-referrer-counts after bulk operations or migrations.referrerCount), not aggregated via PHPCR.Case Sensitivity:
$taxonName = strtolower($taxonName);
Circular References:
doctrine_phpcr:
odm:
logging: true
fix-referrer-counts if counts appear incorrect.Custom Taxon Storage:
Override TaxonManager to use a different storage strategy (e.g., MongoDB for counts).
Hierarchical Tags (Planned):
Implement a workaround with compound keys (e.g., "Laptops > Lenovo > X200" as a single string) until hierarchical support lands.
Static Taxonomies:
Validate @Taxons against a predefined list:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Choice(callback="getAllowedTaxons")
*/
private $taxons;
TaxonManager::bulkCreateTaxons() for large-scale tag initialization:
$taxonManager->bulkCreateTaxons(['tag1', 'tag2'], '/var/www/data/tags');
TaxonObjects queries if used frequently:
$taxon = $dm->getRepository(Taxon::class)->findOneBy(['name' => 'laravel']);
How can I help you explore Laravel packages today?