N
N
nightsamurai2017-12-03 21:23:33
symfony
nightsamurai, 2017-12-03 21:23:33

Why does pagination in doctrine with orderBy c limit/offset/joins give the wrong number of results on DESC?

Hello.
I have the following problem:
There is a class for working with queries, it creates queries for a selection with limit and offset, everything works fine, but if you execute a query for a selection by specifying orderBy = DESC
, the number of results does not match the expected one, for example, by specifying limit=20 and orderBy('column',DESC) will return about 7-8 results
Method - which collects the query:

the code
private function getQuery(string $entityStr): Query
    {
        $entity = $this->getEntity();

        /**
         * @var $manager EntityManager
         */
        $manager = $this->entityManager;

        $query = $manager
            ->createQueryBuilder()
            ->select('e')
            ->from($entityStr, 'e')
        ;

        $this->joinTables($query);

        $this->getWhereBy($query, $entity);

        $this->filterQuery($query, $entity);

        if ($entity->getGetLimit()) {
            $query->setMaxResults($entity->getGetLimit());
        }

        if ($entity->getGetOffset()) {
            $query->setFirstResult($entity->getGetOffset());
        }

        if ($sort = $entity->getGetSort()) {

            $order = $entity->getGetDesc() ? 'DESC' : 'ASC';

            $query->orderBy($this->getSortBy($entity), $order);
        }

        return $query->getQuery();
    }

The method that executes the request:
the code
public function findAllBy(string $entityStr): array
    {
        $query = $this->getQuery($entityStr);

        $paginator = new Paginator($query, true);
        $count = $paginator->count();

        $result = $paginator->getQuery()->getResult();

        return [
            'collection' => $result,
            'has_more'   => \count($result) + $paginator->getQuery()->getFirstResult() < $count,
        ];
    }

PS Locally ( Windows 7, php 7.1.6, doctrine/orm v2.5.12 ) everything works as it should, not on the server ( NGINX, php 7.1.12, doctrine/orm v2.5.12 )
composer
"require": {
        "php": ">=7.1.4",
        "curl/curl": "^1.8",
        "sl4mmer/phpcent":"dev-master",
        "symfony/symfony": "3.3.*",
        "doctrine/orm": "^2.5",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/monolog-bundle": "^3.0.2",
        "symfony/polyfill-apcu": "^1.0",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "^2.0",
        "twig/twig": "^1.0||^2.0",
        "friendsofsymfony/rest-bundle": "^2.1",
        "phpunit/phpunit": "*",
        "friendsofsymfony/oauth-server-bundle": "^1.5",
        "doctrine/doctrine-fixtures-bundle": "^2.3",
        "doctrine/doctrine-migrations-bundle": "^1.0",
        "javiereguiluz/easyadmin-bundle": "^1.16",
        "lexik/jwt-authentication-bundle": "^2.4"
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0"
    },


Any help is welcome :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question