【问题标题】:Symfony form eventlistener string errorSymfony 表单事件监听器字符串错误
【发布时间】:2016-06-09 10:34:25
【问题描述】:

Entity\Profile.php

class Profile
{
    ...
    /**
    * @var string
    *
    * @ORM\Column(name="country", type="string", length=100, nullable=true)
    */
    private $country;

    /**
     * @var string
     *
     * @ORM\Column(name="province", type="string", length=100, nullable=true)
     */
    private $province;
    ...
}

MyProfileTypeForm.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    ...
    ->add('country', CountryType::class, array(
        'label' => 'form.profile.country',
        'preferred_choices' => array(
            'US'
        )
    ))
    ...

    $builder->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
        $form = $event->getForm();
        $country = $form->get('country')->getData();

        $form->add('province', EntityType::class, array(
            'class' => 'UserBundle:LocationProvince',
            'choice_label' => 'name',
            'choice_value' => 'id',
            'query_builder' => function (EntityRepository $er) use ($country) {
                return $er
                    ->createQueryBuilder('l')
                    ->where('l.countryCode = :cc')
                    ->setParameter(':cc', $country);
            },
            'label' => 'form.profile.province',
        ));
    });
}

错误代码:

执行 'UPDATE profile SET 省 = ?哪里 id = ?带参数 [{}, 1]:

可捕获的致命错误:类 Panel\UserBundle\Entity\LocationProvince 的对象无法转换为字符串

说明:

获取国家代码的实体。国家代码绘制的省级列表。但它不记录。

【问题讨论】:

    标签: symfony-forms symfony symfony-3.1


    【解决方案1】:

    在您指定的省份选择下拉列表中:

    'choice_value' => 'id',
    

    但我相信“id”是一个整数。 您可能需要将其更改为:

    'choice_value' => 'province',
    

    试试吧 - 我认为它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      相关资源
      最近更新 更多