如何从 Twig 中的 Symfony2 表单获取 Doctrine2 实体方法

发布于 2024-11-30 17:38:34 字数 283 浏览 1 评论 0原文

我在 Twig 模板中,并且有一个代表 Doctrine2 实体表单的“form”变量。

该实体具有映射到表单中的属性,但该实体还有一些我想从我的 Twig 模板访问的方法。

我想做这样的事情:

{{ form.myMethod }}

或者也许这样的事情:

{{ form.getEntity.myMethod }}

但不幸的是它不起作用。

我怎样才能实现我所需要的?

I'm in a Twig template, and I have a "form" variable that represents a Doctrine2 Entity Form.

This Entity has properties that are mapped into the form, but the Entity has also some methods that I would like to access from my Twig template.

I would love to do something like this:

{{ form.myMethod }}

or maybe something like this:

{{ form.getEntity.myMethod }}

but unfortunately it doesn't work.

How could I achieve what I need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

暗恋未遂 2024-12-07 17:38:34

要从树枝模板中的 FormView 访问您的实体,您可以使用以下代码

{{ form.get('value') }}

,其中 form 是您的 FormView 对象。这将返回您的实体,您可以从那里调用它的任何方法。如果您在表单中嵌入了一组实体或单个实体,您可以以相同的方式访问它

{{ form.someembedform.get('value') }}

{% for obj in form.mycollection %}
  {{ obj.get('value').someMethod }}
{% endif %}

To access your entity from your FormView in a twig template you can use the following code

{{ form.get('value') }}

Where form is your FormView object. This will return your entity and from there you can call any methods on it. If you embed a collection of entities or a single entity in your form you can access it the same way

{{ form.someembedform.get('value') }}

or

{% for obj in form.mycollection %}
  {{ obj.get('value').someMethod }}
{% endif %}
手长情犹 2024-12-07 17:38:34

获取底层实体的更方便的语法是

{{ form.get('value') }}

{{ form.vars.value }}

然后您可以像这样调用任何实体方法:

{{ form.vars.value.someMethod }}

另请参阅 Symfony 文档中的表单章节

An even more convenient syntax to get the underlying entity instead of:

{{ form.get('value') }}

is this:

{{ form.vars.value }}

Then you can call any entity method like this:

{{ form.vars.value.someMethod }}

See also the Form chapter in the Symfony documentation.

旧瑾黎汐 2024-12-07 17:38:34

只是为了更新主题:

form.get('value')

自 symfony 2.1 起已弃用。从 Symfony\Component\Form\FormView 复制:

/*
 * @deprecated Deprecated since version 2.1, to be removed in 2.3. Access
 *             the public property {@link vars} instead.
 */
public function get($name, $default = null) ....

所以,我想

form.vars.value.youMethod()

应该是要走的路。这对我来说很有效。

...这就是我的第一篇文章。希望有帮助!

Just in order to update the subject:

form.get('value')

is deprecated since symfony 2.1. Copy from Symfony\Component\Form\FormView :

/*
 * @deprecated Deprecated since version 2.1, to be removed in 2.3. Access
 *             the public property {@link vars} instead.
 */
public function get($name, $default = null) ....

so, I guess

form.vars.value.youMethod()

should be the way to go. It has worked form me.

... and there it goes my first post here. hope it helps!

迎风吟唱 2024-12-07 17:38:34

花了几个小时试图弄清楚发生了什么以及为什么

{{ form.vars.value }}

是 NULL。

如果您有 form.element (不是表单对象本身)对象,例如,如果您要覆盖已传递 form.row 对象的 form_row 模板,您可以像这样获取实体:

{{ form.getParent().vars.value.MyEntityMethod }}

希望对某人有帮助!

编辑:一年等以后 - 另一个有用的提示:

{% block sonata_type_collection_widget %}
    {% for child in form %}
        {{ child.vars.form.vars.value.name }}
    {% endfor %}
{% endblock %}

Lost few hours trying to figure out what's going on and why

{{ form.vars.value }}

is NULL.

If you have form.element (not the form object itself) object, for example if you are overriding a form_row template that has passed the form.row object, you can get the Entity like this:

{{ form.getParent().vars.value.MyEntityMethod }}

hope that helps someone!

EDIT: Year and so later - another useful tip:

{% block sonata_type_collection_widget %}
    {% for child in form %}
        {{ child.vars.form.vars.value.name }}
    {% endfor %}
{% endblock %}
久隐师 2024-12-07 17:38:34

对象方法应该在树枝中工作,我知道我在一些项目中使用了它们。

尝试使用 ()

{{ form.myMethod() }}

object methods should work in twig, I know I used them in some project.

try to use ()

like {{ form.myMethod() }}

王权女流氓 2024-12-07 17:38:34

似乎在某些时候实际上是null。所以可以

{{ (form.vars.value != null) ? form.vars.value.yourEntityMethod():'' }}

在SF v3.0.6中测试使用。

It seems that at some point the value is actually null. So you can use

{{ (form.vars.value != null) ? form.vars.value.yourEntityMethod():'' }}

tested in SF v3.0.6.

攒一口袋星星 2024-12-07 17:38:34

上述内容在版本 2.6.7 中都不适合我。我使用了自定义表单小部件 来实现这一点:

{# src/AppBundle/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{%- block entity_widget -%}
    <div {{ block('widget_container_attributes') }}>
    {%- for n, child in form %}
        {{- form_widget(child, {
            'entity': form.vars.choices[n].data
        }) -}}
        {{- form_label(child) -}}
    {% endfor -%}
    </div>
{%- endblock %-}

{%- block radio_widget -%}
{# You now have access to entity #}
{%- endblock %-}

None of the above worked for me in version 2.6.7. I used customised form widgets to achieve this:

{# src/AppBundle/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{%- block entity_widget -%}
    <div {{ block('widget_container_attributes') }}>
    {%- for n, child in form %}
        {{- form_widget(child, {
            'entity': form.vars.choices[n].data
        }) -}}
        {{- form_label(child) -}}
    {% endfor -%}
    </div>
{%- endblock %-}

{%- block radio_widget -%}
{# You now have access to entity #}
{%- endblock %-}
心舞飞扬 2024-12-07 17:38:34

使用{{ form.getData.myMethod }}

use {{ form.getData.myMethod }}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文