开发者

Symfony2 - data from a form collection element ends up as arrays instead of Entities

I have two Doctrine entities that have a one-to-many relationship, like this:

License

class License {    
    /**
     * Products this license contains
     * 
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="LicenseProductRelation", mappedBy="license")
     */
    private $productRelations;
}

LicenseProductRelation:

class LicenseProductRelation {
    /**
     * The License referenced by this relation
     * 
     * @var \ISE\LicenseManagerBundle\Entity\License
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="License", inversedBy="productRelations")
     * @ORM\JoinColumn(name="license_id", referencedColumnName="id", nullable=false)
     */
    private $license;
}

And I have this form for the License entity:

class LicenseType extends AbstractType {

    public function buildForm(FormBuilder $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->add('productRelations', 'collection',
            array('type' => new LicenseProductRelationType(),
                  'allow_add' => true,
                  'allow_delete' => true,
                  'prototype' => true,
                  'label' => 'Produkte'));
    }
}

And this form for the LicenseProductRelation entity:

class LicenseProductRelationType extends AbstractType {
    public function buildForm(FormBuilder $builder, array $options) {
        parent::buildForm($builder, $options);
        $builder->add('license', 'hidden');
    }
}

The forms and entities do of course contain other fields, not copied here to keep the post relatively short.

Now when I submit the form and bind the request to the form in my controller, I expect the call $license->getProductRelations() to return an array of LicenseProductRelation obje开发者_StackOverflow中文版cts ($license is the entity passed in to the form, thus the object the request values are written to when I call $form->bindRequest()). Instead, it returns an array of arrays, the inner arrays containing the form field names and values.

Is this normal behaviour or did I make an error that somehow prevents the form component from understanding that License#productRelations shound be an array of LicenseProductRelation objects?


Because your LicenseProductRelationType is an embedded form to LicenseProductType you have to implement the getDefaultOptions method on LicenseProductRelationType and set the data_class to LicenseProductRelation (including its namespace).

See the documentation: http://symfony.com/doc/current/book/forms.html#creating-form-classes

and scroll down to the section entitled "Setting the data_class" - it points out for embedded forms that you need to set up the getDefaultOptions method.

Hope this helps.

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'Acme\TaskBundle\Entity\Task',
    );
}


You have to use the entity type. This one is Doctrine enabled and gives you much love/power to handle collections of entities. Make sure to set "multiple" => true.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜