我的表單中有兩個對象,一個是Scene
及其Background
。大部分頁面是新的Scene
的表單,我有一個角落裏有一個縮略圖和一個文件輸入字段。每當文件字段發生變化時,它就會將圖像上傳到服務器,在該服務器上創建並保留一個實體Background
。然後它返回實體的Id
,我將其存儲在表單的隱藏字段中。Symfony2表單嵌入實體問題
當我提交這個,它告訴我,我試圖存儲一個字符串在Scene#setBackground
方法。如果我從SceneType
表單類中的background
字段中刪除hidden
屬性,則會呈現<select>
框並且一切正常。我添加hidden
屬性,併發布相同的數據,我得到上述錯誤。
SceneType:
class SceneType extends AbstractType {
public function getName() {
return 'scene';
}
public function buildForm (FormBuilder $builder, array $options) {
$builder->add('name');
$builder->add('description');
$builder->add('panoramic', null, array('required' => false));
$builder->add('revealable', null, array('required' => false));
$builder->add('left', 'hidden');
$builder->add('right', 'hidden');
$builder->add('background', 'hidden');
}
}
Relevant section of Entity\Scene:
class Scene {
/**
* @ORM\OneToOne(
* targetEntity="Company\ProductBundle\Entity\Scene\Background",
* inversedBy="scene"
*)
* @ORM\JoinColumn(
* name="scene_background_id",
* referencedColumnName="id",
* nullable=false,
* onDelete="cascade",
* onUpdate="cascade"
*)
*/
protected $background;
public function getBackground() {
return $this->background;
}
public function setBackground (Background $background) {
$this->background = $background;
}
}
Error message:
Catchable Fatal Error: Argument 1 passed to
Company\ProductBundle\Entity\Scene::setBackground() must be an instance of
Company\Company\Entity\Scene\Background, string given, called in
/srv/http/symulator/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php
on line 346 and defined in
/srv/http/symulator/src/Noinc/SimulatorBundle/Entity/Scene.php line 143