簡體   English   中英

如何使用TWIG中的參數訪問實體函數 - symfony2

[英]How to access entity functions with parameters in TWIG - symfony2

我的Symfony2項目中有一個twig模板。 在twig模板中,我有一個實體對象。 此實體對象鏈接到具有oneToMany關系的另一個實體。

例:

{{ submission }} -> Submission entity
{{ submission.histories }} -> Histories entity -> I have here an array collection of histories

實體歷史有一個字段“state_to”

我的目標是只獲取state_to為4的歷史對象

我試着這樣:

{{ submission.histories('status_to', 4)[0] }}

但這不起作用。

我知道我可以使用:

{% for history in submission.histories %}
    {% if history.statusTo == 4 %}
        {{ history.statusDate|date("d F Y") }}
    {% endif %}
{% endfor %}

但我完全相信有更好的方法。

在您的實體中添加方法getHistoryByStatus($status) ,以根據status_to字段過濾您的歷史記錄,然后在您的模板中:

{% set filtered_history = submission.historyByStatus(4)|default(false) %}
{% if filtered_history %}
    {{ filtered_history.statusDate|date("d F Y") }}
{% endif %}

你可以在控制器中調用的方法中找到state_to為4的歷史對象。 然后將其傳遞給視圖。 這個方法可以在你的控制器里面,但是最好把它放在你的歷史存儲庫中嗎? 還是經理..

盡量避免視圖中的復雜性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM