creative-coding/classification-bundle
Installation
Add the bundle to your composer.json:
composer require sonata-project/classification-bundle
Enable it in config/bundle.php:
return [
// ...
Sonata\ClassificationBundle\SonataClassificationBundle::class => ['all' => true],
];
Database Migrations
Run the provided migrations (located in vendor/sonata-project/classification-bundle/migrations) to create tables for:
SonataClassificationNode (taxonomy nodes)SonataClassificationNodeTranslation (translations)SonataClassificationNodeHierarchy (hierarchy relationships)First Use Case: Basic Taxonomy
Define a taxonomy in a Doctrine entity (e.g., ProductCategory):
use Sonata\ClassificationBundle\Model\ClassificationNodeInterface;
/**
* @ORM\Entity
*/
class ProductCategory implements ClassificationNodeInterface
{
// Implement required methods from ClassificationNodeInterface
// ...
}
Admin Configuration
Register the admin class in config/packages/sonata_admin.yaml:
sonata_admin:
options:
models:
ProductCategory: App\Entity\ProductCategory
Creating a Taxonomy
Assigning Taxonomies to Entities
Link entities (e.g., Product) to taxonomy nodes via a many-to-many relationship:
/**
* @ORM\ManyToMany(targetEntity="ProductCategory")
* @ORM\JoinTable(name="product_category")
*/
private $categories;
Querying by Taxonomy Fetch products under a specific category:
$products = $entityManager->getRepository(Product::class)
->findBy(['categories' => $categoryNode]);
Dynamic Taxonomy Rendering Use Twig to display taxonomy paths:
{% for node in product.categories %}
{{ node.getPathAsString() }} →
{% endfor %}
Sonata\ClassificationBundle\Form\Type\ClassificationNodeType to add custom validation or fields.use Sonata\ClassificationBundle\Serializer\ClassificationNodeNormalizer;
$serializer->normalize($node, null, [
new ClassificationNodeNormalizer(),
]);
sonata.classification.node.pre_persist or sonata.classification.node.pre_update to modify nodes before saving.Hierarchy Depth Limits
getPath() is 10. Increase via:
$node->setMaxDepth(20);
Translation Conflicts
sonata_classification_node_translation table has unique constraints on (node_id, locale).php bin/console cache:clear
Admin Route Conflicts
config/routes.yaml:
sonata_classification:
resource: '@SonataClassificationBundle/Resources/config/routing/admin.xml'
prefix: /admin/classification
Lazy-Loading Issues
$query->leftJoin('node.children', 'children')
->addSelect('children');
Enable Debug Mode: Add to config/packages/dev/sonata_classification.yaml:
sonata_classification:
debug: true
Logs hierarchy operations to var/log/dev.log.
Check Node States:
$node->isRoot(); // Is this the root node?
$node->isLeaf(); // Has no children?
$node->getParent(); // Get parent node.
Custom Node Types
Extend Sonata\ClassificationBundle\Model\ClassificationNode to add custom logic:
class CustomNode extends ClassificationNode
{
private $customField;
// Add getters/setters and override methods as needed.
}
Override Templates
Copy templates from vendor/sonata-project/classification-bundle/Resources/views to templates/SonataClassificationBundle to customize admin UI.
Add Validation Use Symfony’s constraints on taxonomy nodes:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @UniqueEntity(fields={"name", "locale"}, message="Duplicate node name.")
*/
class ProductCategory implements ClassificationNodeInterface
{
// ...
}
How can I help you explore Laravel packages today?