2016-12-13 32 views
0

如果我擁有一個ID的不同項目,如何將項目分組到列表中?TWIG的羣組項目

我想如果循環找到兩個或多個具有相同ID的項目將它們合併到列表ul > li

例如:

enter image description here

{% extends 'base.html.twig' %} 

{% block wrapper %} 
    <div class="panel panel-default"> 
     <div class="panel-heading clearfix add"> 
      Заявки 
     </div> 
     <table class="table"> 
      <tr> 
       <th>ID</th> 
       <th>ID заявки</th> 
       <th>Склад</th> 
       <th>Матеріал</th> 
       <th>Кількість</th> 
       <th>Сумма</th> 
       <th>Дата</th> 
       <th>Час</th> 
       <th>Користувач</th> 
      </tr> 
      {% if itemEntries == null %} 
       <tr> 
        <td colspan="6">Елементи для показу відсутні</td> 
       </tr> 
      {% endif %} 
      {% for value in itemEntries %} 
       <tr> 
        <td>{{ value.id }}.</td> 
        <td>{{ value.bid }}</td> 
        <td>{{ value.storage.name }}</td> 
        <td>{{ value.item.name }} (ID: {{ value.item }})</td> 
        <td>{{ value.count }}</td> 
        <td>-{{ value.count * value.item.price }}</td> 
        <td>{{ value.date | date('d-m-Y') }}</td> 
        <td>{{ value.time | date('H:m:i') }}</td> 
        <td>{{ value.userLogin }}</td> 
       </tr> 
      {% endfor %} 
     </table> 
    </div> 
{% endblock %} 

謝謝!

+1

基本上這個工作應該在PHP來實現,而不是(通過使用SQL查詢或'array_filter()')。僅使用Twig模板來表示。見@穆罕默德的回答。 – yceruto

+0

@Yonel非常感謝你! – ladone

回答

1

最好的方法是將項目分組到SQL查詢中。

所以在你的控制器中,你應該使用groupBy()來分組結果。

請看下面的例子:

$itemEntries = $this->em->createQueryBuilder() 
     ->select('e') 
     ->from('AppBundle:Entry', 'e') 
     ->groupBy('e.bidId') 
     ->getQuery() 
     ->getResult(); 
+0

非常感謝你! – ladone