From bc2476f342ceda98559b87a314806581cf969796 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 29 Aug 2012 15:14:14 +0200 Subject: [PATCH] [DDC-1918] Fix weird results at the end of paginator when using fetch joins --- .../ORM/Tools/Pagination/Paginator.php | 20 +++--- .../ORM/Functional/Ticket/DDC1885Test.php | 4 +- .../ORM/Functional/Ticket/DDC1918Test.php | 62 +++++++++++++++++++ 3 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1918Test.php diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index bb1514ea0..16c9dcfb0 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -174,16 +174,18 @@ class Paginator implements \Countable, \IteratorAggregate $whereInQuery = $this->cloneQuery($this->query); // don't do this for an empty id array - if (count($ids) > 0) { - $namespace = WhereInWalker::PAGINATOR_ID_ALIAS; + if (count($ids) == 0) { + return new \ArrayIterator(array()); + } - $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); - $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids)); - $whereInQuery->setFirstResult(null)->setMaxResults(null); - foreach ($ids as $i => $id) { - $i++; - $whereInQuery->setParameter("{$namespace}_{$i}", $id); - } + $namespace = WhereInWalker::PAGINATOR_ID_ALIAS; + + $whereInQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\ORM\Tools\Pagination\WhereInWalker')); + $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids)); + $whereInQuery->setFirstResult(null)->setMaxResults(null); + foreach ($ids as $i => $id) { + $i++; + $whereInQuery->setParameter("{$namespace}_{$i}", $id); } $result = $whereInQuery->getResult($this->query->getHydrationMode()); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php index 8fbb19ad0..9d158155d 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1885Test.php @@ -5,8 +5,6 @@ namespace Doctrine\Tests\ORM\Functional\Ticket; use Doctrine\Tests\Models\Quote\Group; use Doctrine\Tests\Models\Quote\User; -require_once __DIR__ . '/../../../TestInit.php'; - /** * @group DDC-1845 * @group DDC-1885 @@ -170,4 +168,4 @@ class DDC1885Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $user->getGroups()->get(0)); $this->assertInstanceOf('Doctrine\Tests\Models\Quote\Group', $user->getGroups()->get(1)); } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1918Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1918Test.php new file mode 100644 index 000000000..64b3ad48d --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1918Test.php @@ -0,0 +1,62 @@ +useModelSet('cms'); + parent::setUp(); + } + + public function testLastPageCorrect() + { + $groups = array(); + for ($i = 0; $i < 3; $i++) { + $group = new CmsGroup(); + $group->name = "test"; + $this->_em->persist($group); + + $groups[] = $group; + } + + for ($i = 0; $i < 10; $i++) { + $user = new CmsUser(); + $user->username = "user$i"; + $user->name = "user$i"; + $user->status = "active"; + $user->groups = $groups; + + $this->_em->persist($user); + } + + $this->_em->flush(); + + $query = $this->_em->createQuery('SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g'); + $query->setFirstResult(6); + $query->setMaxResults(3); + + $paginator = new Paginator($query, true); + $this->assertEquals(3, count(iterator_to_array($paginator))); + + $query->setFirstResult(8); + $query->setMaxResults(3); + + $paginator = new Paginator($query, true); + $this->assertEquals(2, count(iterator_to_array($paginator))); + + $query->setFirstResult(10); + $query->setMaxResults(3); + + $paginator = new Paginator($query, true); + $this->assertEquals(0, count(iterator_to_array($paginator))); + } +}