在Symfony上使用SubCategories创建Category的形式 [英] Create form of Category with SubCategories on Symfony
本文介绍了在Symfony上使用SubCategories创建Category的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个实体:Category和SubCategory.一个类别有0个或多个子类别.它们与多对一关系相关.我不敢想象我怎么能像这样按子类别来创建带有子类别的表单:
I have two entities : Category and SubCategory. One category have 0 or many subcategory. They are related with Many-to-One relation. I don't imagine how i can make a form with subcategories group by category like this :
CATEGORY 1 :
- SubCategory 1
- SubCategory 2
CATEGORY 2:
- SubCategory 1
- SubCategory 2
我的实际表格:
class CategorieType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('sous_categories', CollectionType::class, array(
'entry_type' => SousCategorie::class,
'entry_options' => array('label' => false),
))
;
}
感谢您的帮助
推荐答案
您可以在存储库函数中使用groupBy:
You can use a groupBy in you repository function :
$builder
->add('sous_categories', EntityType::class, array(
'class' => 'AppBundle:Bdd\SousCategorie',
'query_builder' => function(EntityRepository $er) use ($revue) {
return $er->createQueryBuilder('sc')
->addSelect('sc')
->join('sc.Categorie', 'c')
->andWhere('c.Revue = :Revue')
->setParameter('Revue', $revue)
->orderBy('c.libelle', 'ASC')
->addOrderBy('sc.libelle', 'ASC')
->groupBy('sc.Categorie')
;
},
'choice_label' => function($sousCategorie){
return $sousCategorie->getCategorie()->getLibelle()." - ".$sousCategorie->getLibelle();
},
'multiple' => true,
'expanded' => true,
))
;
这篇关于在Symfony上使用SubCategories创建Category的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文