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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7134216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:06:58  来源:igfitidea点击:

How to get a Doctrine2 Entity method from a Symfony2 Form in Twig

phpsymfonytwig

提问by David Morales

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

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

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.

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

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?

我怎样才能达到我所需要的?

回答by dturcotte

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

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

{{ 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 是您的 FormView 对象。这将返回您的实体,然后您可以在其上调用任何方法。如果您在表单中嵌入一组实体或单个实体,您可以以相同的方式访问它

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

or

或者

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

回答by Philipp Rieber

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 Symfony2 Docs:

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

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

回答by javigzz

Just in order to update the subject:

只是为了更新主题:

form.get('value')

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

自 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) ....

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!

...这是我的第一篇文章。希望能帮助到你!

回答by Anton Valqk

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.element(不是表单对象本身)对象,例如,如果您要覆盖通过 form.row 对象的 form_row 模板,您可以像这样获得实体:

{{ 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 %}

回答by Inoryy

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

对象方法应该在树枝中工作,我知道我在某个项目中使用过它们。

try to use ()

尝试使用 ()

like {{ form.myMethod() }}

喜欢 {{ form.myMethod() }}

回答by Nicolas Castro

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

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

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

tested in SF v3.0.6.

在 SF v3.0.6 中测试。

回答by Jonny

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

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 %-}

回答by julesbou

use {{ form.getData.myMethod }}.

使用{{ form.getData.myMethod }}.