Symfony2 - 来自表单集合元素的数据最终以数组而不是实体 [英] Symfony2 - data from a form collection element ends up as arrays instead of Entities

查看:107
本文介绍了Symfony2 - 来自表单集合元素的数据最终以数组而不是实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



执照

pre> 类许可证{
/ **
*此许可证的产品包含
*
* @var \Doctrine\Common\ Collections \ArrayCollection
* @ ORM \OneToMany(targetEntity =LicenseProductRelation,mappedBy =license)
* /
private $ productRelations;
}

LicenseProductRelation:

  class LicenseProductRelation {
/ **
*此关系引用的许可证
*
* @var \ISE\LicenseManagerBundle\\ \\ Entity \ License
* @ ORM\Id
* @ ORM\ManyToOne(targetEntity =License,inversedBy =productRelations)
* @ ORM\JoinColumn(name =license_id,referencedColumnName =id,nullable = false)
* /
private $ license;
}

我有这个表格用于License实体:

  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'));




$ b $ p
$ b

此表单为LicenseProductRelation实体:

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


表单和实体当然包含其他字段,现在,当我提交表单并将请求绑定到我的控制器中的表单时,我期望调用 $ license-> getProductRelations()返回一个LicenseProductRelation对象数组( $ license )是传入表单的实体,因此当我调用 $ form-> bindRequest())时,请求值被写入的对象。相反,它返回一个数组数组,内部数组包含表单字段名称和值。

这是正常行为,还是我犯了一个错误,以某种方式阻止窗体组件了解 License#productRelations shound是一个LicenseProductRelation对象的数组吗?

解决方案

因为您的 LicenseProductRelationType 是一个嵌入式表单,所以您必须实现 getDefaultOptions LicenseProductRelationType 并将 data_class 设置为 LicenseProductRelation code>(包括它的命名空间)。

请参阅文档: http://symfony.com/doc/current/book/forms.html#creating-form-classes



并向下滚动到标题为设置data_class的部分 - 它指向ou t用于需要设置getDefaultOptions方法的嵌入式表单。



希望这有助于您。

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


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 objects ($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',
    );
}

这篇关于Symfony2 - 来自表单集合元素的数据最终以数组而不是实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆