如何从Twig中的Symfony2表单获取Doctrine2实体方法 [英] How to get a Doctrine2 Entity method from a Symfony2 Form in Twig
问题描述
我在Twig模板中,并且有一个表示"Doctrine2实体表单"的表单"变量.
I'm in a Twig template, and I have a "form" variable that represents a Doctrine2 Entity Form.
该实体具有映射到表单中的属性,但是该实体还有一些我想从我的Twig模板访问的方法.
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.
我想做这样的事情:
{{ form.myMethod }}
或者也许是这样的:
{{ form.getEntity.myMethod }}
但不幸的是,它不起作用.
but unfortunately it doesn't work.
我怎样才能实现我所需要的?
How could I achieve what I need?
推荐答案
要在树枝模板中从FormView访问实体,可以使用以下代码
To access your entity from your FormView in a twig template you can use the following code
{{ form.get('value') }}
其中form是您的FormView对象.这将返回您的实体,从那里您可以在其上调用任何方法.如果您在表单中嵌入实体集合或单个实体,则可以用相同的方式访问
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') }}
或
{% for obj in form.mycollection %}
{{ obj.get('value').someMethod }}
{% endif %}
这篇关于如何从Twig中的Symfony2表单获取Doctrine2实体方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!