I am encountering a serious problem with setMaxResult and setFirstResult .
When I try to get the result without setMaxResults and setFirstResult , it works fine, all rows are returned.
When I use offset = 0 and limit = 10, it works well, 10 rows are returned.
When I use offset = 10 and limit = 10, it returns 5 rows (should be 7)
Another example: I used offset = 0, limit = 20, it returned 15 rows. But it should be 17 lines.
With offset = 0 and limit = 30, it returned all 17 rows .... Why does this query work so badly? With offset = 0 and a limit of 20, he should have returned all 17 lines ... but not 15 ..
The code:
$eligibleCircles = $this->getAllCircles($user);
$results = $this->getEntityManager()
->createQuery(
'SELECT
e
FROM
TestBundle:Event e
LEFT JOIN
e.eligibleCircles eligibleCircles
WHERE
(
eligibleCircles in (:eligibleCircles)
OR
e.owner = :user
)
AND
e.eventStatus = :eventStatus
AND
NOT EXISTS (
SELECT
eh
FROM
TestBundle:EventHidden eh
WHERE
eh.user = :user
AND
eh.event = e
)
AND
e.startDate < :currentDate
ORDER BY e.startDate DESC
'
)
->setParameter('eventStatus', 3)
->setParameter('eligibleCircles', $eligibleCircles )
->setParameter('user', $user )
->setParameter('currentDate', new \DateTime('now') )
->setFirstResult($offset)
->setMaxResults($limitNr)
->getResult();