-2

Now i hav this. How do I group by the field hotel.name? And yes, I saw this post, but it does not work. 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 %}

I need get this: enter image description here

We were advised to use the groop_by, but I do not understand how.

   {% 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 %}

Controller:

 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);
    }
Dmitry
  • 27
  • 6
  • Possible duplicate of [Twig foreach group by date](https://stackoverflow.com/questions/23896701/twig-foreach-group-by-date) – Wolen Nov 12 '17 at 10:10
  • Why don't you prepare your data in your controller so they are display to display in Twig? – Tokeeen.com Nov 12 '17 at 10:11
  • 2
    Possible duplicate of [Group by loop parametrs](https://stackoverflow.com/questions/47243016/group-by-loop-parametrs) – BENARD Patrick Nov 12 '17 at 13:01
  • 1
    Don't ask two times the same question please... You can create a custom twig extension which makes a 'asort' php function with your objects collection. – BENARD Patrick Nov 12 '17 at 13:01

1 Answers1

-1

Answer:

   {% 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 %}
Dmitry
  • 27
  • 6
  • While this may be the right answer, is don't contain any explanation. Please explain it, otherwise it might be removed or downvoted. – Stephan Vierkant Nov 22 '17 at 16:26