简体   繁体   English

Symfony2:具有两个id属性的表单元素

[英]Symfony2 : form element with two id attributes

I was trying to build an embedded form with Symfony2/Twig in which I wanted to display the id of the mapped entity for each line on the form. 我试图用Symfony2 / Twig构建一个嵌入式表单,其中我想在表单上显示每行的映射实体的id。
I was doing this : 我这样做:

{% for p in form.products %}
    <tr>
        <td>{{p.vars.data.id}}</td>
    </tr>
{% endfor %}

which is very simple ... but nothing was printed. 这很简单......但没有打印出来。 I tried the following : 我尝试了以下方法:

{% for p in form.products %}
    <tr>
        <td>{{dump(p.vars)}</td>
    </tr>
{% endfor %}

And the result was unexpected : 结果出人意料:

array:27 [▼   "value" => ItemProduct {#857 ▼
    -id: null
    #enabled: false
    #commission: 0.0
    #support: 1
    -quantity: 0
    -id: 1   }

Two id attributes for the element. 元素的两个id属性。 I didn't know it was even possible and I don't understand how this can happen, my code is extremly simple and I never encountered such issues before even with far more complex embedded forms. 我不知道它是否可能,我不明白这是怎么发生的,我的代码极其简单,我甚至在使用更复杂的嵌入式表格之前从未遇到过这样的问题。

Here is the rest of my code : 这是我的其余代码:

ItemProductType ItemProductType

class ItemProductType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder -> add ( 'artist_commission' , 'text' );
    }
    // ...
}

ItemProductsType ItemProductsType

class ItemProductsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('products', 'collection', array('type' => new ItemProductType()));
}

ItemProduct ItemProduct

/**
 * @ORM\Table(name="item_products")
 * @ORM\Entity(repositoryClass="APIBundle\Entity\ItemProductRepository")
 */
class ItemProduct extends Product
{
    /**
     * @var integer
     *
     * @ORM\Id()
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $enabled;

    /**
     * @ORM\Column(type="float")
     */
    protected $commission;

    /**
     * @ORM\Column(type="integer")
     */
    protected $support;
}

I think I've finally found what was wrong (writing the question probably made me have a new look on my code). 我想我终于找到了什么问题(写这个问题可能让我对我的代码有了新的认识)。

My ItemProduct inherits a Product class which has its own id attribute. 我的ItemProduct继承了一个具有自己的id属性的Product类。 Since there are two distinct tables and the id attribute is not merged properly Symfony makes the two different id attributes visible. 由于有两个不同的表并且未正确合并id属性,Symfony使两个不同的id属性可见。
As only one of them was actually linked to the data, the other was null. 由于其中只有一个实际上与数据相关联,另一个则为空。

Even if the problem is solved and revealed larger issues in my code I am still interested in explanation about this behaviour and how it is possible to make to attributes with the same name coexist in an entity. 即使问题得到解决并在我的代码中发现更大的问题,我仍然对这种行为的解释感兴趣,以及如何使实体中具有相同名称的属性共存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM