Paginator count returns zero when using instance of #5875

Open
opened 2026-01-22 15:20:47 +01:00 by admin · 10 comments
Owner

Originally created by @pedrobrost on GitHub (Feb 5, 2018).

I have a query with something like this:

$qb = $this->createQueryBuilder('load')
            ->select('load')
            ->andWhere('load INSTANCE OF :type')
            ->setParameter('type', $this->getEntityManager()->getClassMetadata('\GRAL\LoadBundle\Entity' . $type))
            ->setFirstResult(($page - 1) * self::MAX_PER_PAGE)
            ->setMaxResults(self::MAX_PER_PAGE)
            ->orderBy('load.createdAt');

return new Paginator($qb->getQuery(), true);

It works perfectly when I iterate over the paginator, the results are correct, but the count result is always zero:

count($paginator); // returns 0
paginator|length <!-- returns 0 -->

This only happens when I use INSTANCE OF. I'm using single table inheritance.

Originally created by @pedrobrost on GitHub (Feb 5, 2018). I have a query with something like this: ```php $qb = $this->createQueryBuilder('load') ->select('load') ->andWhere('load INSTANCE OF :type') ->setParameter('type', $this->getEntityManager()->getClassMetadata('\GRAL\LoadBundle\Entity' . $type)) ->setFirstResult(($page - 1) * self::MAX_PER_PAGE) ->setMaxResults(self::MAX_PER_PAGE) ->orderBy('load.createdAt'); return new Paginator($qb->getQuery(), true); ``` It works perfectly when I iterate over the paginator, the results are correct, but the `count` result is always zero: ```php count($paginator); // returns 0 ``` ```twig paginator|length <!-- returns 0 --> ``` This only happens when I use `INSTANCE OF`. I'm using single table inheritance.
admin added the Bug label 2026-01-22 15:20:47 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 5, 2018):

Can you check the generated SQL?

@Ocramius commented on GitHub (Feb 5, 2018): Can you check the generated SQL?
Author
Owner

@pedrobrost commented on GitHub (Feb 5, 2018):

SELECT m0_.id AS id_0, m0_.loadDate AS loadDate_1, m0_.createdAt AS createdAt_2, m0_.store_id AS store_id_3, m0_.is_draft AS is_draft_4, m0_.gang_id AS gang_id_5, m0_.dest_store_id AS dest_store_id_6, m0_.discr AS discr_7, m0_.owner_id AS owner_id_8 FROM materials_load m0_ WHERE (m0_.discr IN (?)) AND m0_.discr IN ('gangLoad', 'storeLoad') ORDER BY m0_.createdAt ASC LIMIT 15 OFFSET 0
@pedrobrost commented on GitHub (Feb 5, 2018): ```sql SELECT m0_.id AS id_0, m0_.loadDate AS loadDate_1, m0_.createdAt AS createdAt_2, m0_.store_id AS store_id_3, m0_.is_draft AS is_draft_4, m0_.gang_id AS gang_id_5, m0_.dest_store_id AS dest_store_id_6, m0_.discr AS discr_7, m0_.owner_id AS owner_id_8 FROM materials_load m0_ WHERE (m0_.discr IN (?)) AND m0_.discr IN ('gangLoad', 'storeLoad') ORDER BY m0_.createdAt ASC LIMIT 15 OFFSET 0 ```
Author
Owner

@Ocramius commented on GitHub (Feb 5, 2018):

And what if you iterate over results? What is the query? Are there results at all?

@Ocramius commented on GitHub (Feb 5, 2018): And what if you iterate over results? What is the query? Are there results at all?
Author
Owner

@Ocramius commented on GitHub (Feb 5, 2018):

Overall, this looks like an issue in inheritance and criteria applied to it, but we'd need to have a failing test case to attack it. Can you please see if writing one similar to the ones in https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket is feasible on your end?

@Ocramius commented on GitHub (Feb 5, 2018): Overall, this looks like an issue in inheritance and criteria applied to it, but we'd need to have a failing test case to attack it. Can you please see if writing one similar to the ones in https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket is feasible on your end?
Author
Owner

@pedrobrost commented on GitHub (Feb 5, 2018):

If I iterate over results I get all as expected, the count is the only problem. I will see how to write one of that tests, first time ;)

I'd like to mention this:

In Doctrine\ORM\Tools\Pagination class, in getCountQuery() if I replace this:

if ($this->useOutputWalker($countQuery)) {
    $platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win

    $rsm = new ResultSetMapping();
    $rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');

    $countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker');
    $countQuery->setResultSetMapping($rsm);
} else {
    $this->appendTreeWalker($countQuery, 'Doctrine\ORM\Tools\Pagination\CountWalker');
}

for only this:

$this->appendTreeWalker($countQuery, 'Doctrine\ORM\Tools\Pagination\CountWalker');

It works perfectly (I don't understand what is happening).

Thanks, and sorry for my english, I will see how to create the test

@pedrobrost commented on GitHub (Feb 5, 2018): If I iterate over results I get all as expected, the `count` is the only problem. I will see how to write one of that tests, first time ;) I'd like to mention this: In `Doctrine\ORM\Tools\Pagination` class, in `getCountQuery()` if I replace this: ```php if ($this->useOutputWalker($countQuery)) { $platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win $rsm = new ResultSetMapping(); $rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count'); $countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker'); $countQuery->setResultSetMapping($rsm); } else { $this->appendTreeWalker($countQuery, 'Doctrine\ORM\Tools\Pagination\CountWalker'); } ``` for only this: ```php $this->appendTreeWalker($countQuery, 'Doctrine\ORM\Tools\Pagination\CountWalker'); ``` It works perfectly (I don't understand what is happening). Thanks, and sorry for my english, I will see how to create the test
Author
Owner

@Ocramius commented on GitHub (Feb 5, 2018):

@pedrobrost that at least isolates the bug to Doctrine\ORM\Tools\Pagination\CountOutputWalker

Also, make sure you try your test against 2.6 and master :-)

@Ocramius commented on GitHub (Feb 5, 2018): @pedrobrost that at least isolates the bug to `Doctrine\ORM\Tools\Pagination\CountOutputWalker` Also, make sure you try your test against `2.6` and `master` :-)
Author
Owner

@pedrobrost commented on GitHub (Feb 5, 2018):

I have not done the test yet, but I think the problem is setting the parameter, as @eheuje says here, if we set the parameter using getClassMetadata(), the count query won't work.

@pedrobrost commented on GitHub (Feb 5, 2018): I have not done the test yet, but I think the problem is setting the parameter, as @eheuje says [here](https://github.com/doctrine/doctrine2/issues/4462#issuecomment-181933308), if we set the parameter using `getClassMetadata()`, the `count` query won't work.
Author
Owner

@sdespont commented on GitHub (May 2, 2018):

This problem still exists in version 2.6.1

@sdespont commented on GitHub (May 2, 2018): This problem still exists in version 2.6.1
Author
Owner

@jerkan commented on GitHub (Nov 21, 2018):

I found a workaround using queryBuilder expr method:
->where($queryBuilder->expr()->isInstanceOf('alias', Classname::class))

@jerkan commented on GitHub (Nov 21, 2018): I found a workaround using queryBuilder expr method: ` ->where($queryBuilder->expr()->isInstanceOf('alias', Classname::class)) `
Author
Owner

@xAzoom commented on GitHub (Aug 23, 2022):

This problem still exists :/

@xAzoom commented on GitHub (Aug 23, 2022): This problem still exists :/
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5875