[英]Save symfony2 entity form with entity field
我有一個城市添加表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', null, array(
'label' => 'City',
));
$builder->add('country', 'genemu_jqueryautocompleter_entity', array(
'class' => 'X\tBundle\Entity\Country',
'property' => 'name',
'route_name' => 'ajax_country',
'required' => true,
'label' => 'Country',
));
}
國家是一個實體。 當我提交此表單時-S2返回錯誤: cannot save because cannot convert Object to String
為此,我使用魔術方法__toString()
,它返回(string)$this->getId()
;
但是我不確定-對嗎?
答案: Symfony /理論:持久存在時,“可捕獲的致命錯誤:類<type>的對象無法轉換為字符串”
因此,我們使用下一件事:
/**
* @var X\tBundle\Entity\Country;
*
* @ORM\ManyToOne(targetEntity="X\tBundle\Entity\Country")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
*/
public $country;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.