Symfony2 - 表单和 Twig 模板中的整个子实体 [英] Symfony2 - Entire child entity in form and Twig template

查看:30
本文介绍了Symfony2 - 表单和 Twig 模板中的整个子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Sonata 管理表单中,我想显示与当前对象相关的所有图像.不需要编辑它们.我设法使用实体字段类型及其属性选项来选择图像 URL 作为标签,以便我可以调用模板中的所有图片:

->add('image', 'entity', array('类' =>'Hop\HopBundle\Entity\Image','财产' =>'image_url','query_builder' =>函数($er)使用($object){返回 $er->createQueryBuilder('i')->where('i.object = :object')->setParameter('object', $object);},'扩展' =>真的,))

.

{% for child in form %}<img src="{{ child.vars.label }}" alt="{{ child.vars.label }}"/>{% 结束为 %}

这是因为实体类型似乎只给模板一个标签和一个值.

但我还想获得图像宽度、描述、日期……换句话说:我们如何在 Twig 模板中获得每个完整的相关图像对象?

谢谢.

解决方案

所以我在谷歌上搜索了很多,这对我有用(Symfony 2.2)
更新为@flu 在评论中建议

{% for key, child in form %}<div>{% set entity = form.vars.choices[key].data %}{{ form_widget(child) }}{{ form_label(child) }}

{% 结束为 %}

entity 是子对象.从 Symfony 2.3 开始,只有 form.vars.data,如果我理解正确的话:https://github.com/symfony/symfony/pull/5023

In a Sonata admin form, I'd like to display all images related to the current object. Don't need to edit them. I managed to get this working with an entity field type and its property option to select the image URL as label so I can call all pictures in the template :

->add('image', 'entity', array(
    'class' => 'Hop\HopBundle\Entity\Image',
    'property' => 'image_url',
    'query_builder' => function($er) use ($object) {
        return $er->createQueryBuilder('i')
        ->where('i.object = :object')
        ->setParameter('object', $object)
        ;
    },
    'expanded' => true,
))

.

{% for child in form %}
    <img src="{{ child.vars.label }}" alt="{{ child.vars.label }}" />
{% endfor %}

It's because it seems that the entity type only give label and one value to the template.

But I'd like to get also image width, descriptions, date, ... In other word : how can we get each entire related image object in the Twig template ?

Thanks.

解决方案

So I googled it a lot and here's what worked for me (Symfony 2.2)
Updated as @flu suggested in a comment

{% for key, child in form %}
<div>
    {% set entity = form.vars.choices[key].data %}
    {{ form_widget(child) }}
    {{ form_label(child) }}
</div>
{% endfor %}

and entity is the child object. As of Symfony 2.3 there will be just form.vars.data, if I understand correctly: https://github.com/symfony/symfony/pull/5023

这篇关于Symfony2 - 表单和 Twig 模板中的整个子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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