问题
I try to make a form with category and subcategory like the picture below :
So, I made my form like that :
->add('souscategorie', EntityType::class, array(
'label' => false,
'class' => 'App:souscategorie',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('souscategorie')
->leftJoin('souscategorie.categorie', 'categorie')
->addSelect('souscategorie')
->addSelect('categorie')
;
},
'expanded'=> true,
'multiple'=> true,
'choice_label' => function($sousCategorie){
return $sousCategorie->getCategorie()->getNom()." - ".$sousCategorie->getNom();
},
'group_by' => function($sousCategorie, $key, $value){
return $sousCategorie->getCategorie()->getNom();
}))
The result show all categories and subcategories concatenate.
How to split categories and subcategories ?
Thanks for your help ;)
回答1:
To make tree
structure for your categories
you can use StofDoctrineExtensionsBundle
Documentation for the bundle is here
You would need to use Tree
extension (nestedset variation).
Here is the example that shows how to add categories to the project
来源:https://stackoverflow.com/questions/52802530/symfony-form-category-subcategory