[英]How to access entity functions with parameters in TWIG - symfony2
I have a twig template in my Symfony2 project. 我的Symfony2项目中有一个twig模板。 In the twig template, I have an entity object.
在twig模板中,我有一个实体对象。 This entity object is linked to another entity with oneToMany relation.
此实体对象链接到具有oneToMany关系的另一个实体。
Example: 例:
{{ submission }} -> Submission entity
{{ submission.histories }} -> Histories entity -> I have here an array collection of histories
The entity histories has a field "state_to" 实体历史有一个字段“state_to”
My goal is to get only the histories object where the state_to is 4 我的目标是只获取state_to为4的历史对象
I tryed like that: 我试着这样:
{{ submission.histories('status_to', 4)[0] }}
But this is not working. 但这不起作用。
I know that I can use: 我知道我可以使用:
{% for history in submission.histories %}
{% if history.statusTo == 4 %}
{{ history.statusDate|date("d F Y") }}
{% endif %}
{% endfor %}
But I am allmost sure that there is a nicer way. 但我完全相信有更好的方法。
Add a method getHistoryByStatus($status)
in your entity to filter your histories based on the status_to
field, then in your template: 在您的实体中添加方法
getHistoryByStatus($status)
,以根据status_to
字段过滤您的历史记录,然后在您的模板中:
{% set filtered_history = submission.historyByStatus(4)|default(false) %}
{% if filtered_history %}
{{ filtered_history.statusDate|date("d F Y") }}
{% endif %}
you could just find the histories object where the state_to is 4 within a method called in your controller. 你可以在控制器中调用的方法中找到state_to为4的历史对象。 then pass it to the view.
然后将其传递给视图。 This method can be inside your controller, but is better to have it in your history repository maybe?
这个方法可以在你的控制器里面,但是最好把它放在你的历史存储库中吗? or a manager..
还是经理..
try avoiding complexity in the views. 尽量避免视图中的复杂性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.