实体\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 Province = 时发生异常?哪里 id = ?带参数 [{}, 1]:
可捕获的致命错误:类 Panel\UserBundle\Entity\LocationProvince 的对象无法转换为字符串
描述:
获取国家代码的实体。国家代码绘制的省级列表。但它不记录。