Cocolabs-SAS / cocorico

👐 Cocorico is an open source marketplace solution for services and rentals. More information right here: https://www.cocorico.io/ 🚀 Cocorico is also available in an off-the-shelf SaaS package, check out https://www.hatch.li to launch your platform today. 😍 We are hiring (telecommute welcome 🏡): https://www.welcometothejungle.com/en/companies/cocorico/jobs/candidatures-spontanees#apply
https://www.cocolabs.com
MIT License
857 stars 610 forks source link

Search result #501

Closed gonzasylv closed 4 years ago

gonzasylv commented 4 years ago
Q A
Bug report? [ ]
Feature request? [ ]
Support request? [ x ]

hi @cocolabssas ,

I would like the result of a search to be in descending order, that is to say that the result is from recent articles to older ones.

i found:

src/Cocorico/CoreBundle/Controller/Frontend/ListingSearchController.php

 /* @var ListingSearchRequest $listingSearchRequest /
        $listingSearchRequest = $this->get('cocorico.listing_search_request');
        $isXmlHttpRequest ? $listingSearchRequest->setSortBy('distance') : null;
        $form = $this->createSearchResultForm($listingSearchRequest);

It seems to be sorted by distance

src/Cocorico/CoreBundle/Model/Manager/ListingSearchManager.php

        switch ($listingSearchRequest->getSortBy()) {
            case 'price':
                $queryBuilder->orderBy("l.price", "ASC");
                break;
            case 'distance':
                $queryBuilder->orderBy("distance", "ASC");
                break;
            default:
                $queryBuilder->orderBy("distance", "ASC");
                break;
        }

How I can simply modify the code so that the search result is from the most recent to the oldest as for the list of ads on the home page ?

Thanks

SchulzWill commented 4 years ago

Just change the default option to $queryBuilder->addOrderBy("l.createdAt", "DESC");

SchulzWill commented 4 years ago

@gonzasylv , you should put it as the first order by in your query, which will render it before the other ordinators you will use... so, in your ListingSearchManager.php, in the search method you could put the ->addOrderBy('l.createdAt', 'desc'); before switch ($listingSearchRequest->getSortBy()) { case 'price': $queryBuilder->orderBy("l.price", "ASC"); break; case 'distance': $queryBuilder->orderBy("distance", "ASC"); break; default: $queryBuilder->orderBy("distance", "ASC"); break; }

gonzasylv commented 4 years ago
        //Order
        $queryBuilder->addOrderBy('l.createdAt', 'desc');
        switch ($listingSearchRequest->getSortBy()) {
            case 'price':
                $queryBuilder->orderBy("l.price", "ASC");
                break;
            case 'distance':
                $queryBuilder->orderBy("distance", "ASC");
                break;
            default:
                $queryBuilder->orderBy("distance", "ASC");
                break;
        }

The result is the same @SchulzWill :/

The result of my search gives: http://imgur.com/wBn7UPUl.png , it puts "Marrakech" in the first search results while my ip is from Casablanca

SchulzWill commented 4 years ago

Have you seen how the query is being mounted? The first one is that the order clauses will be inputted together to the database. You can activate your MySql trace to see how the query is being executed there and identify the order used to input your ordenation

SchulzWill commented 4 years ago

Hello, @gonzasylv . This depends on your MySQL version. You could try changing your MYSQL config as explained in here: https://stackoverflow.com/a/25080306/3618037