1
我正試圖用Symfony2/Twig構建一個嵌入式表單,其中我想顯示錶單上每行的映射實體的id。
我這樣做:Symfony2:具有兩個id屬性的表單元素
{% for p in form.products %}
<tr>
<td>{{p.vars.data.id}}</td>
</tr>
{% endfor %}
這是很簡單的......但沒有打印。 我試過如下:
{% for p in form.products %}
<tr>
<td>{{dump(p.vars)}</td>
</tr>
{% endfor %}
,結果是出乎意料:該元素
array:27 [▼ "value" => ItemProduct {#857 ▼
-id: null
#enabled: false
#commission: 0.0
#support: 1
-quantity: 0
-id: 1 }
兩個id
屬性。我不知道這甚至是可能的,我不明白這是怎麼發生的,我的代碼極其簡單,即使有更復雜的嵌入式表單,我也從未遇到過這樣的問題。
這裏是我的代碼的其餘部分:
ItemProductType
class ItemProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder -> add ('artist_commission' , 'text');
}
// ...
}
ItemProductsType
class ItemProductsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('products', 'collection', array('type' => new ItemProductType()));
}
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;
}
你可以接受你自己的答案,這將標記爲解決這個問題:http://stackoverflow.com/help/self-answer –