我想以一种特殊的方式显示表单选择(单选按钮)。
简短的解释:
我需要在选择小部件中显示实体的许多属性,而不仅仅是名称(_toString)和值(id)。
扩展解释:
我不会浪费时间解释我的实体,因为它们工作正常,我对它们没有任何问题。
我有一个SalonWeb Entity,它与Album Entity有OneToOne 关系。此外,相册实体与Foto 实体具有OneToMany 关系,并包含一个 $fotos ArrayCollection 和一个链接 $foto_id 的 $foto_principal 属性。
因此,通过正确的学说查询,我可以访问如下内容:
$salon_web->getAlbum()->getFotoPrincipal();
或者,在 TWIG 中:
salonWeb.album.fotoPrincipal
到现在为止都是正确的。
我想将这张照片(英文照片)显示为表单选择标签,所以我做了这个代码(正在工作)
在表单生成器中:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('salones', 'entity', array(
'class' => 'CommonBundle:SalonWeb',
'required' => true,
'expanded' => true,
'query_builder' => function(EntityRepository $er)
{
return $er->getQueryBuilderVisiblesContacto();
},
'property' => 'album.foto_principal'
))
// More code...
}
...在 TWIG 模板中:
<div>
{{ form_errors(formulario.salones) }}
{{ form_label(formulario.salones) }}
{% for childSalon in formulario.salones %}
<label><img src="/uploads/galeria/{{ childSalon.vars.label }}" alt="" />{{ form_widget(childSalon) }}</label>
{% endfor %}
</div>
{{ form_widget(formulario) }}
直到这里一切正常。但问题是我只能在表单选择中显示一个属性(在本例中是 SalonWeb 实体的 album.foto_principal 属性)
我想展示这样的东西:
<div>
{{ form_errors(formulario.salones) }}
{{ form_label(formulario.salones) }}
{% for childSalon in formulario.salones %}
<label><img src="/uploads/galeria/{{ childSalon.whatever.name }}" alt="" />{{ childSalon.whatever.address ~ ' ' ~ childSalon.whatever.anotherSalonWebProperty }}
<div>{{ childSalon.whatever.theLastProperty }}</div>
{{ form_widget(childSalon) }}</label>
{% endfor %}
</div>
{{ form_widget(formulario) }}