开发者

Symfony2 Form embedded entity issue

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. It then returns the Id of the entity, which I store in a hidden field in the form.

When I submit this, it tells me that I'm trying to store a string in the Scene#setBackground method. If I remove the hidden attribute from the background field in the SceneType form class, it renders a <select> box and all is fine. I add the hidden attribute, and post the same data, I get the above error.

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->a开发者_开发百科dd('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.

I do not think passing hidden id's in forms is necessary; you should be able to persist that association outside the form. Hope you consider this approach.

If you must use your way, you'll need to make a BackgroundType form, then add that form to the SceneType form:

$builder->add('background', new BackgroundType());

I assume the BackgroundType() will render a hidden id field then.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜