按对象字段分组

时间:2017-11-12 10:05:55

标签: php symfony view group-by

现在我有这个。如何按字段hotel.name分组? 是的,我看过这篇文章,但它不起作用。 Twig Loop Grouping enter image description here

  {% for item in pagination.items %}   <!-- Вывод результатов по отелям -->
            <div class="row">
                <div class="col-lg-1">{{ loop.index }}</div>
                <div class="col-lg-6">
                    <span class="text-capitalize">{{ item.hotel.name }}</span>
                <span class="text-primary">{{ item.roomName }}</span>
                    {% if(item.mealName) %}
                        <span class="text-muted">({{ item.mealName }})</span>
                    {% endif %}
                </div>
                <div class="col-lg-2">{{ item.price }} {{ item.currency }}</div>
            </div>
{% endfor %}

我需要得到这个: enter image description here

我们被建议使用groop_by,但我不明白如何。

   {% for item,
        group in pagination|group_by(=>_.hotel.name)
        %}   <!-- Вывод результатов по отелям -->
            <div class="row">
                <div class="col-lg-1">{{ loop.index }}</div>
                <div class="col-lg-6">
                    <span class="text-capitalize">{{ item.hotel.name }}</span>
                    <span class="text-primary">{{ item.roomName }}</span>
                    {% if(item.mealName) %}
                        <span class="text-muted">({{ item.mealName }})</span>
                    {% endif %}
                </div>
                <div class="col-lg-2">{{ item.price }} {{ item.currency }}</div>
            </div>
        {% endfor %}

控制器:

 public function resultsAction($searchId, $page)
    {
        $em = $this->getDoctrine()->getManager();
        $objSearchRequest = $em->find('UtsHotelBundle:SearchRequest', $searchId);
        if(!$objSearchRequest){
            $this->createNotFoundException();
        }

        $objSearchForm = $this->createForm('uts_hotel_search_request', $objSearchRequest);
        $templateVars = array(
            'searchForm' => $objSearchForm->createView(),
            'request' => $objSearchRequest
        );
        if($objSearchRequest->isComplete() || $objSearchRequest->isOld()){
            $repository = $em->getRepository('UtsHotelBundle:SearchResult');
            $query = $repository->createQueryForPagination($searchId);
            $paginator  = $this->get('knp_paginator');
            $pagination = $paginator->paginate($query, $page, 50);
            $templateVars['pagination'] = $pagination;
        }

        return $this->render('UtsHotelBundle:Default:results.html.twig', $templateVars);
    }

1 个答案:

答案 0 :(得分:-1)

答案:

   {% set date = null %}
    {% set otelindex=0 %}
    {% for item in pagination.items %}   <!-- Вывод результатов по отелям -->
        {% if date !=  item.hotel.name %}
            {% set date = item.hotel.name  %}
            {%  set otelindex=otelindex+1 %}
            <div class="row">
            <div class="col-lg-6">
                <h4>{{ otelindex }}{{ ". " }}{{item.hotel.name}}</h4>
            </div>
                <div class="col-lg-2">
                <h4>  {{ item.mealName }}{{ item.price }} {{ item.currency }}</h4>
                </div>
            </div>
        {% endif %}
        <div class="row">

            <div class="col-lg-6">

                <span class="text-primary">{{ item.roomName }}</span>
                {% if(item.mealName) %}
                    <span class="text-muted">({{ item.mealName }})</span>
                {% endif %}
            </div>
            <div class="col-lg-2">{{ item.price }} {{ item.currency }}</div>
        </div>
    {% endfor %}