简体   繁体   English

Symfony 2,在实体上使用ChoiceType的createFormBuilder,choice_label是int

[英]Symfony 2, createFormBuilder with ChoiceType on Entity, choice_label is int

I have a little problem with Symfony 2 and the form builder. 我对Symfony 2和表单构建器有一点问题。 I want to create a ChoiceType field based on Doctrine findAll result. 我想基于Doctrine findAll结果创建一个ChoiceType字段。

My choices are entities array, but on the choice_label function, the first variable is int ! 我的选择是实体数组,但在choice_label函数中,第一个变量是int!

I put a little code for explain : 我把一些代码解释一下:

    $categories = $categoryRepository->findAll();

    foreach ($categories as $value) {
        echo "category name : ".$value->getName()."<br/>";
    }
    /* Result :
        category name : First
        category name : Second
     */        

    $form = $this->createFormBuilder($dance)
            ->add('name', TextType::class, array('label' => 'Nom de la dance'))
            ->add('description', TextareaType::class, array('label' => 'Description'))
            ->add('creationDate', DateTimeType::class, array('label' => 'Date de création'))
            ->add('category', ChoiceType::class, [
                'choices' => $categories,
                'choice_label' => function($category, $key, $index) {
                    var_dump($category);
                    // Result : int(0) 
                    return $category->getName();
                    // Exception !
                },
                'choice_attr' => function($category, $key, $index) {
                    return ['class' => $category->getId()];
                },
            ])
            ->add('save', SubmitType::class, array('label' => 'Sauvegarder'))
            ->getForm();

Of course, I have a Fatal error: Call to a member function getName() on integer ... 当然,我有一个致命错误:在整数上调用成员函数getName()...

Someone can help me on this problem ? 有人可以帮我解决这个问题吗?

Thank you ! 谢谢 !

If you're using an older version than Symfony 2.7, you can not simply pass an array of objects to the choices option. 如果您使用的是比Symfony 2.7更旧的版本,则不能简单地将一组对象传递给choices选项。 This is only supported in Symfony >=2.7. 仅在Symfony> = 2.7中支持此功能。 If you want to do this in Symfony 2.7 or 2.8, you have to activate the choices_as_values option : 如果要在Symfony 2.7或2.8中执行此操作,则必须激活choices_as_values选项

'choices_as_values' => true

By default, in Symfony 2.x choices is built the other way around: the keys of the array become the value and the value of they array becomes the label . 默认情况下,在Symfony 2.x中, choices是以相反的方式构建的: 数组的键成为值,并且数组的值成为标签 So, you'll get 0 for the first element in your array. 因此,对于数组中的第一个元素,您将获得0。 :) :)

Alternatively, you may want to use the EntityType class instead of ChoiceType . 或者,您可能希望使用EntityType而不是ChoiceType It will pass the actual object to the function in any case. 它会在任何情况下将实际对象传递给函数。

Furthermore, if you want to specify a property of your entity (or a referenced entity) as your label, you can also use property paths : 此外,如果要将实体(或引用的实体)的属性指定为标签,还可以使用属性路径

'choice_label' => 'name'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM