Installation:
composer require astina/labels-bundle.AstinaLabelsBundle and StofDoctrineExtensionsBundle in AppKernel.php.stof_doctrine_extensions in config.yml to enable translatable entities.First Use Case:
labels property to a Doctrine entity (e.g., Product):
/**
* @ORM\ManyToMany(targetEntity="Astina\Bundle\LabelsBundle\Entity\Label")
*/
private $labels;
LabelCategory and Label entities (e.g., via DoctrineFixturesBundle).Quick Form Integration:
astina_labels form type in a form builder:
$builder->add('labels', 'astina_labels', [
'categories' => ['color', 'size'],
'label' => 'Filters',
'required' => false,
]);
Workflow:
ManyToMany relationship between your entity and Astina\Bundle\LabelsBundle\Entity\Label.LabelCategory and Label entities.$product->addLabel($label);
$entityManager->persist($product);
Translations:
Gedmo\Translatable for multilingual labels (configured via stof_doctrine_extensions)./**
* @Gedmo\TranslationEntity(class="Astina\Bundle\LabelsBundle\Entity\LabelTranslation")
*/
class Label {}
astina_labels field type to render checkboxes/radio buttons for each category.categories to restrict displayed labels (e.g., ['color', 'material']).$builder->add('labels', 'astina_labels', [
'categories' => ['color'],
'label' => 'Select Colors',
'required' => false,
'multiple' => true,
]);
Service Configuration:
services.yml:
app.filter_search.product:
class: Astina\Bundle\LabelsBundle\Search\LabelSearch
arguments:
- @astina_labels.repository.label
- @app.repository.product
TranslatedLabelSearch for multilingual support.Querying:
$search = $container->get('app.filter_search.product');
$results = $search->search(new \Astina\Bundle\LabelsBundle\Search\SearchQuery(['red', 'blue']));
Sonata\AdminBundle\Admin\Admin to add label management:
protected function configureFormFields(FormMapper $formMapper) {
$formMapper->add('labels', 'astina_labels', [
'categories' => ['color', 'size'],
]);
}
Missing Fixtures:
LabelCategory and Label entities.Translations Not Working:
stof_doctrine_extensions is configured with translatable: true and default_locale set.php bin/console cache:clear) after enabling translations.Form Field Not Rendering:
categories aren’t passed to the form type, no labels will display.categories in the form builder.Deprecated Symfony Version:
Empty Search Results:
SearchQuery contains valid label names (case-sensitive).ManyToMany.Translation Domain Issues:
choice_translation_domain is set in the form type (handled in v0.4.0).Custom Label Priorities:
priority property in Label to sort labels (added in v0.3.0):
$label->setPriority(1); // Lower numbers appear first
Override Search Logic:
Astina\Bundle\LabelsBundle\Search\LabelSearch to customize query building.Add New Label Types:
Tag) and replicate the Label structure for reuse.Index Labels:
label and label_category tables for faster searches:
CREATE INDEX idx_label_name ON label(name);
CREATE INDEX idx_entity_labels ON entity_labels(entity_id, label_id);
Cache Search Results:
LabelSearch service output if labels change infrequently:
services:
app.filter_search.product:
class: Astina\Bundle\LabelsBundle\Search\LabelSearch
arguments:
- @astina_labels.repository.label
- @app.repository.product
calls:
- [setCache, ['@cache.app']]
How can I help you explore Laravel packages today?