Pagination: countQuery overrides existing ResultSetMapping #5360

Open
opened 2026-01-22 15:05:26 +01:00 by admin · 5 comments
Owner

Originally created by @nikolajlovenhardt on GitHub (Dec 22, 2016).

Hello,

I have an issue regarding the count method in the doctrine paginator.

Snippet

public function foo()
{
    $queryBuilder->setParameter('entityClass', $this->getEntityManager()->getClassMetadata(EntityClass::class));

    $queryBuilder->andWhere(sprintf('%s instance of :entityClass', $this->getName()));

    $query = $queryBuilder->getQuery();
}

The issue
The getCountQuery method in Doctrine\ORM\Tools\Pagination\Paginator overrides the result mapping in line 251. It is not appropriate because it overwrites the existing result mapping, and therefore in my case returns a count of 0.

So the Paginator has results, but the count is zero.

I'm not sure how to fix this, because the getResultMapping method in the Query is private.

Thank you in advance

Originally created by @nikolajlovenhardt on GitHub (Dec 22, 2016). Hello, I have an issue regarding the count method in the doctrine paginator. **Snippet** ```php public function foo() { $queryBuilder->setParameter('entityClass', $this->getEntityManager()->getClassMetadata(EntityClass::class)); $queryBuilder->andWhere(sprintf('%s instance of :entityClass', $this->getName())); $query = $queryBuilder->getQuery(); } ``` **The issue** The `getCountQuery` method in `Doctrine\ORM\Tools\Pagination\Paginator` overrides the result mapping in line 251. It is not appropriate because it overwrites the existing result mapping, and therefore in my case returns a count of 0. So the Paginator has results, but the count is zero. I'm not sure how to fix this, because the `getResultMapping` method in the Query is private. Thank you in advance
admin added the Bug label 2026-01-22 15:05:26 +01:00
Author
Owner

@Ocramius commented on GitHub (Dec 23, 2016):

Linking logic that overwrites the mappings

@nikolajlovenhardt I don't see any custom mappings in your case? Can you make this an isolated test case?

@Ocramius commented on GitHub (Dec 23, 2016): Linking [logic that overwrites the mappings](https://github.com/doctrine/doctrine2/blob/cd1a5fcadce6cd5fe3ee17360503d73e82852339/lib/Doctrine/ORM/Tools/Pagination/Paginator.php#L252-L256) @nikolajlovenhardt I don't see any custom mappings in your case? Can you make this an isolated test case?
Author
Owner

@nikolajlovenhardt commented on GitHub (Dec 23, 2016):

@Ocramius

No you are right - I don't add any custom mappings. But when debugging I found out that changing $rsm = new ResultSetMapping(); into $rsm = $countQuery->getResultMapping() seemed to be fixing the problem. But that's not possible since the getResultMapping method is protected.

I've made an example below
https://gist.github.com/nikolajlovenhardt/dcaee906027a57e8bb0ee4f8f29c8816

@nikolajlovenhardt commented on GitHub (Dec 23, 2016): @Ocramius No you are right - I don't add any custom mappings. But when debugging I found out that changing `$rsm = new ResultSetMapping();` into `$rsm = $countQuery->getResultMapping()` seemed to be fixing the problem. But that's not possible since the getResultMapping method is protected. I've made an example below https://gist.github.com/nikolajlovenhardt/dcaee906027a57e8bb0ee4f8f29c8816
Author
Owner

@Ocramius commented on GitHub (Dec 23, 2016):

I understand the issue even less now, or the issue description doesn't
reflect the test that you linked.

I'll try running the test locally though.

On 23 Dec 2016 08:55, "Nikolaj Løvenhardt Petersen" <
notifications@github.com> wrote:

@Ocramius https://github.com/Ocramius

I don't add any custom mappings, but I tried to change the
getResultMapping method to public, and change $rsm = new
ResultSetMapping(); into $rsm = $countQuery->getResultMapping(), and that
fixed it.

I've made an example below
https://gist.github.com/nikolajlovenhardt/dcaee906027a57e8bb0ee4f8f29c8816


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6189#issuecomment-268953024,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakCbjkP5eCZBsNalU1Rioid65NXMQks5rK36HgaJpZM4LT4Bv
.

@Ocramius commented on GitHub (Dec 23, 2016): I understand the issue even less now, or the issue description doesn't reflect the test that you linked. I'll try running the test locally though. On 23 Dec 2016 08:55, "Nikolaj Løvenhardt Petersen" < notifications@github.com> wrote: > @Ocramius <https://github.com/Ocramius> > > I don't add any custom mappings, but I tried to change the > getResultMapping method to public, and change $rsm = new > ResultSetMapping(); into $rsm = $countQuery->getResultMapping(), and that > fixed it. > > I've made an example below > https://gist.github.com/nikolajlovenhardt/dcaee906027a57e8bb0ee4f8f29c8816 > > — > You are receiving this because you were mentioned. > Reply to this email directly, view it on GitHub > <https://github.com/doctrine/doctrine2/issues/6189#issuecomment-268953024>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAJakCbjkP5eCZBsNalU1Rioid65NXMQks5rK36HgaJpZM4LT4Bv> > . >
Author
Owner

@Ocramius commented on GitHub (Dec 23, 2016):

Converted into a failing test case in https://github.com/doctrine/doctrine2/pull/6194

@Ocramius commented on GitHub (Dec 23, 2016): Converted into a failing test case in https://github.com/doctrine/doctrine2/pull/6194
Author
Owner

@Ocramius commented on GitHub (Dec 23, 2016):

I applied the patch to #6194, but exposing the resultset mapping is a no-go from here.

Will need an alternate approach instead, or checking why the new mapping doesn't fit.

@Ocramius commented on GitHub (Dec 23, 2016): I applied the patch to #6194, but exposing the resultset mapping is a no-go from here. Will need an alternate approach instead, or checking why the new mapping doesn't fit.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5360