Symfony表单中实体的组复选框

时间:2017-11-10 14:11:15

标签: symfony

如何为Symfony表单中的实体分组复选框?

->add(
    'products',
    EntityType::class,
    [
        'class' => Product::class,
        'choice_label' => 'titel',
        'multiple' => true,
        'expanded' => true,
    ]
)

我的域模型Product只属于一个ProductFamily。每个ProductFamily只属于一个ProductSuperFamily。在我的表单中,Product复选框应按如下方式分组:

Product Super Family 1
    Product Family 1
        [ ] Product 1
        [ ] Product 2
    Product Family 2
        [ ] Product 3
    Product Family 3
        [ ] Product 4
        [ ] Product 5
Product Super Family 2
    Product Family 4
        [ ] Product 6
        [ ] Product 7
        [ ] Product 8
    Product Family 5
        [ ] Product 9
Product Super Family 3
    Product Family 6
        [ ] Product 10
        [ ] Product 11

我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

选择类型允许,您必须使用the group_by option

'group_by' => function(Product $product) {
    return $product->getFamily()->getLabel()
},