没有插件的Symfony 4.1树形视图表单

时间:2018-11-13 22:21:47

标签: forms symfony treeview

如何通过以下方式在EntityType控件中显示元素:

Group 1
parent
  - child
  -- second_child
  - child
  - child
  -- second_child
Group 2
  parent
   - child
   -- second_child
   ------third_child
parent
   - child

我有一个与其自身相关的表,该实体的父字段定义了下一个子元素的父字段和子字段,而level字段则确定嵌套的深度。 我试图用这种结构中的元素手动填充数组,但未将其写入数据库:

$data = [];
$data = ['Group 1']['parent'] = $entityElement;
$data = ['Group 1']['- child'] = $entityElement->getChildren()[0];
$data = ['Group 1']['-- second_child'] = $entityElement->getChildren()[1];

->add('elements', ChoiceType::class, [

    'mapped' => false,
    'expanded' => false,
    'multiple' => true,

    'choices' => $options['elements'],
    'placeholder' => false,
])

我尝试为此创建查询,但无法正常工作,即使我添加addOrderBy('c.parent')

        ->add('elements', EntityType::class, [
            'class' => Category::class,
            'choice_label' => function($item) { return $item->getIndentName(); },
            'expanded' => false,
            'multiple' => true,
            'query_builder' => function (ElementRepository $er) {
                return $er->createQueryBuilder('e')->groupBy('e.parent');
            },
            'group_by' => function ($choiceValue) {
                if (is_null($choiceValue->getBrand())) {
                    return '';
                }
                return $choiceValue->getBrand()->getName();
            },
        ])

我应该怎么做?

0 个答案:

没有答案