2013-05-06 28 views
0

我試圖顯示一個實體字段類型展開和多個設置爲true。但是,我想顯示實體的其他屬性。這與Symfony2 : accessing entity fields in Twig with an entity field type類似,但該解決方案尚未適用於我。我收到錯誤:「」的項目「代碼」不存在。訪問實體字段類型的樹枝中的其他屬性

如何訪問實體的其他屬性(本例中爲顏色)?

這是我到目前爲止有:

$builder->add('colors', 'entity', array(
      'class' => 'PrismPortalCommonBundle:Color', 
      'property' => 'code', 
      'expanded' => true, 
      'multiple' => true, 
     )); 

,並在樹枝的模板:

{% for color in form.colors %} 
<tr> 
    <td>{{ form_widget(color) }}</td> 
    <td>{{ color.vars.data.code }}</td> 
</tr> 
{% endfor %} 
+0

請檢查什麼{{轉儲(color.vars.data)}}返回 – Nisam 2013-05-06 06:36:30

+0

轉儲整個'form.colors'請。看起來你有「空」的對象 – DonCallisto 2013-05-06 07:05:11

回答

0

在Symfony的2.5 - 您可以通過使用從每個選擇訪問數據的孩子做到這一點指數值。

在表單生成器 - 如你所料:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    // Generate form 
    $builder 
     ->add('child', 'entity', array(
      'class'   => 'MyBundle:Child', 
      'label'   => 'Children', 
      'property'  => 'any_property_for_label', 
      'expanded'  => true, 
      'multiple'  => true 
     )); 
} 

在樹枝模板:

{{ form_start(form) }} 
{% for child in form.child %} 
    {% set index = child.vars.value %}{# get array index #} 
    {% set entity = form.child.vars.choices[index].data %}{# get entity object #} 
    <tr> 
     <td>{{ form_widget(child) }}</td>{# render checkbox #} 
     <td>{{ entity.name }}</td> 
     <td>{{ entity.email }}</td> 
     <td>{{ entity.otherProperty }}</td> 
    </tr> 
{% endfor %} 
{{ form_end(form) }} 
相關問題