Symfony form category - subcategory

心不动则不痛 提交于 2021-01-29 06:01:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!