我的表单中有两个对象,一个 Scene
和它的 Background
.页面的大部分是新的Scene
的表格,我有一个角落,那里有一个缩略图和一个文件输入字段。每当文件字段更改时,它会将图像上传到服务器,其中 Background
实体被创建并持久化。然后它返回 Id
实体,我将其存储在表单的隐藏字段中。
当我提交这个时,它告诉我我正试图在 Scene#setBackground
中存储一个字符串。方法。如果我删除 hidden
来自 background
的属性SceneType
中的字段表单类,它呈现一个 <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
最佳答案
I have two objects in my form, a Scene and its Background. The majority of the page is the form for the new Scene, and I have a corner where there is a thumbnail and a file input field. Whenever the file field is changed, it uploads the image to the server where a Background entity is created and persisted. The Scene then gets associated to that background. Now, with the background set, I only need to worry about modifying my Scene's properties via a form.
我不认为在表单中传递隐藏的 id 是必要的;您应该能够在表单之外保留该关联。希望您考虑这种方法。
如果您必须使用自己的方式,则需要创建一个 BackgroundType 表单,然后将该表单添加到 SceneType 表单:
$builder->add('background', new BackgroundType());
我假设 BackgroundType() 将呈现一个隐藏的 id 字段。
关于doctrine-orm - Symfony2 表单嵌入实体问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7035230/