根据EntityType中的ID选择的值

时间:2019-07-14 13:04:04

标签: symfony-3.4 formbuilder

我希望EntityType(DefaulList)根据数据实体中的ID选择默认值。

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('mpkId', EntityType::class, array(
                'class' => MpkList::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        ->orderBy('u.id', 'ASC');
                },
                'choice_label' => 'mpk'
            ))
            ->add("time", IntegerType::class)
            ->add("submit", SubmitType::class);
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(["data_class"=>Data::class]);
    }

示例:数据['mpkId'] = 1,MpkList ['id'=> 1,'mpk'=>'description']。格式为“说明”的选定字段

1 个答案:

答案 0 :(得分:0)

您可以使用回调函数。并访问您的MpkList类中的所有对象。 像这样

   ->add('mpkId', EntityType::class, array(
                'class' => MpkList::class,
                'choice_label' => function (MpkList $MpkList) {
                    return "id=".$MpkList->getId()."-Description=". $MpkList->getMpkId()->getDescription();

                },
            ))