doctrine-orm - Symfony2 表单嵌入实体问题

标签 doctrine-orm symfony symfony-forms

我的表单中有两个对象,一个 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/

相关文章:

php - Symfony 3 通过重定向传递对象

php - 将 SQL 查询转换为 Doctrine,Error

symfony2 : how to use group_concat in QueryBuilder

php - 为联合学说 2 集合实现 setter/getter

symfony - 在 where 条件下的学说 ORM 计数数组集合

php - Maker Bundle 仅支持注释映射

symfony - 奏鸣曲3 : Deleting entities in OneToMany relationship doesn't work with 'by_reference' => false

symfony - 数组属性的等效 IN 子句

unit-testing - Symfony2 最佳实践、业务逻辑和单元测试

symfony - 如何处理Symfony2表单中EntityChoiceList中的选择 "other"?