From 42aff75108a856c24489d4614a9ac5708b4f7e3a Mon Sep 17 00:00:00 2001 From: Merixstudio Date: Thu, 20 Nov 2014 15:28:12 +0100 Subject: [PATCH 01/13] Fixed counting exception Fixed "Invalid parameter number: number of bound variables does not match number of tokens " exception during execution count on Query where select part of query contains :parameters. --- lib/Doctrine/ORM/Tools/Pagination/Paginator.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index 087a17513..b3c264467 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -140,6 +140,16 @@ class Paginator implements \Countable, \IteratorAggregate $countQuery->setFirstResult(null)->setMaxResults(null); try { + $parser = new Query\Parser($countQuery); + $parameterMappings = $parser->parse($parser)->getParameterMappings(); + $parameters = $countQuery->getParameters(); + foreach($parameters as $k=>$param){ + if(!array_key_exists($param->getName(), $parameterMappings)){ + $parameters->remove($k); + } + } + $countQuery->setParameters($parameters); + $data = $countQuery->getScalarResult(); $data = array_map('current', $data); $this->count = array_sum($data); From d02ea224dfc7fac9d12f1e48911ae31d7448ed75 Mon Sep 17 00:00:00 2001 From: Merixstudio Date: Mon, 24 Nov 2014 15:27:15 +0100 Subject: [PATCH 02/13] Allowed to get count query from paginator. --- .../ORM/Tools/Pagination/Paginator.php | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index b3c264467..4fcf1885d 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -118,38 +118,8 @@ class Paginator implements \Countable, \IteratorAggregate public function count() { if ($this->count === null) { - /* @var $countQuery Query */ - $countQuery = $this->cloneQuery($this->query); - - if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) { - $countQuery->setHint(CountWalker::HINT_DISTINCT, true); - } - - 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'); - } - - $countQuery->setFirstResult(null)->setMaxResults(null); - + $countQuery = $this->getCountQuery(); try { - $parser = new Query\Parser($countQuery); - $parameterMappings = $parser->parse($parser)->getParameterMappings(); - $parameters = $countQuery->getParameters(); - foreach($parameters as $k=>$param){ - if(!array_key_exists($param->getName(), $parameterMappings)){ - $parameters->remove($k); - } - } - $countQuery->setParameters($parameters); - $data = $countQuery->getScalarResult(); $data = array_map('current', $data); $this->count = array_sum($data); @@ -161,6 +131,39 @@ class Paginator implements \Countable, \IteratorAggregate return $this->count; } + public function getCountQuery(){ + /* @var $countQuery Query */ + $countQuery = $this->cloneQuery($this->query); + + if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) { + $countQuery->setHint(CountWalker::HINT_DISTINCT, true); + } + + 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'); + } + + $countQuery->setFirstResult(null)->setMaxResults(null); + $parser = new Query\Parser($countQuery); + $parameterMappings = $parser->parse($parser)->getParameterMappings(); + $parameters = $countQuery->getParameters(); + foreach($parameters as $k=>$param){ + if(!array_key_exists($param->getName(), $parameterMappings)){ + $parameters->remove($k); + } + } + $countQuery->setParameters($parameters); + return $countQuery; + } + /** * {@inheritdoc} */ From fa014ee55ded1bc598ce98c9fa30ad201db5b52f Mon Sep 17 00:00:00 2001 From: Merixstudio Date: Mon, 24 Nov 2014 15:28:37 +0100 Subject: [PATCH 03/13] Test parameter removing parameters passed to select part of query. --- .../Tests/ORM/Functional/PaginationTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index bf9e4a685..487a57577 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -150,6 +150,43 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertCount(1, $paginator->getIterator()); $this->assertEquals(1, $paginator->count()); } + + public function testCountQuery_ParametersInSelect() + { + /** @var $query Query */ + $query = $this->_em->createQuery( + 'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < :id ORDER BY promotedFirst' + ); + $query->setParameter('vipMaxId', 10); + $query->setParameter('id', 100); + $query->setFirstResult(null)->setMaxResults(null); + + $this->assertEquals( + "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (CASE WHEN c0_.id < ? THEN 1 ELSE 0 END) AS sclr4, c0_.email_id AS email_id5 FROM cms_users c0_ WHERE c0_.id < ? ORDER BY sclr4 ASC", + $query->getSQL() + ); + $paginator = new Paginator($query); + $countQuery = $paginator->getCountQuery(); + + $this->assertEquals( + "SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id0 FROM (SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (CASE WHEN c0_.id < ? THEN 1 ELSE 0 END) AS sclr4, c0_.email_id AS email_id5 FROM cms_users c0_ WHERE c0_.id < ? ORDER BY sclr4 ASC) dctrn_result) dctrn_table", + $countQuery->getSQL() + ); + $this->assertEquals(2, count($countQuery->getParameters())); + $this->assertEquals(3, $paginator->count()); + + + $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker'); + $paginator = new Paginator($query); + $countQuery = $paginator->getCountQuery(); + $this->assertEquals( + "SELECT count(DISTINCT c0_.id) AS sclr0 FROM cms_users c0_ WHERE c0_.id < ?", + $countQuery->getSQL() + ); + //if select part of query is replaced with count(...) paginator should remove parameters from query object not used in new query. + $this->assertEquals(1, count($countQuery->getParameters())); + $this->assertEquals(3, $paginator->count()); + } public function populate() { From de1843ad305c4607691ae81377b40b0237f0eb5d Mon Sep 17 00:00:00 2001 From: Merixstudio Date: Mon, 24 Nov 2014 16:07:22 +0100 Subject: [PATCH 04/13] Testing SQLs in functional test is not necessary --- .../Tests/ORM/Functional/PaginationTest.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index 487a57577..ae3c34260 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -161,28 +161,14 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $query->setParameter('id', 100); $query->setFirstResult(null)->setMaxResults(null); - $this->assertEquals( - "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (CASE WHEN c0_.id < ? THEN 1 ELSE 0 END) AS sclr4, c0_.email_id AS email_id5 FROM cms_users c0_ WHERE c0_.id < ? ORDER BY sclr4 ASC", - $query->getSQL() - ); $paginator = new Paginator($query); $countQuery = $paginator->getCountQuery(); - - $this->assertEquals( - "SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id0 FROM (SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (CASE WHEN c0_.id < ? THEN 1 ELSE 0 END) AS sclr4, c0_.email_id AS email_id5 FROM cms_users c0_ WHERE c0_.id < ? ORDER BY sclr4 ASC) dctrn_result) dctrn_table", - $countQuery->getSQL() - ); $this->assertEquals(2, count($countQuery->getParameters())); $this->assertEquals(3, $paginator->count()); - $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker'); $paginator = new Paginator($query); $countQuery = $paginator->getCountQuery(); - $this->assertEquals( - "SELECT count(DISTINCT c0_.id) AS sclr0 FROM cms_users c0_ WHERE c0_.id < ?", - $countQuery->getSQL() - ); //if select part of query is replaced with count(...) paginator should remove parameters from query object not used in new query. $this->assertEquals(1, count($countQuery->getParameters())); $this->assertEquals(3, $paginator->count()); From 18244161f733a8e32ffbd1626ac66dbb44675602 Mon Sep 17 00:00:00 2001 From: Merixstudio Date: Mon, 24 Nov 2014 16:41:16 +0100 Subject: [PATCH 05/13] Filtering by auto-increnement field causes test error. Executing the same test many times causes error because AI fields. --- tests/Doctrine/Tests/ORM/Functional/PaginationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index ae3c34260..fd9b2d08d 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -155,7 +155,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase { /** @var $query Query */ $query = $this->_em->createQuery( - 'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < :id ORDER BY promotedFirst' + 'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < :id or 1=1 ORDER BY promotedFirst' ); $query->setParameter('vipMaxId', 10); $query->setParameter('id', 100); From af39fce6e58717feab19d7a9f9066a785cd36415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kolanowski?= Date: Thu, 27 Nov 2014 08:11:27 +0100 Subject: [PATCH 06/13] Missing doc block, removed parse() parameter. Missing doc block, removed parse() parameter. --- lib/Doctrine/ORM/Tools/Pagination/Paginator.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index 4fcf1885d..dc4801a46 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -119,6 +119,7 @@ class Paginator implements \Countable, \IteratorAggregate { if ($this->count === null) { $countQuery = $this->getCountQuery(); + try { $data = $countQuery->getScalarResult(); $data = array_map('current', $data); @@ -130,8 +131,13 @@ class Paginator implements \Countable, \IteratorAggregate return $this->count; } - - public function getCountQuery(){ + /** + * Returns Query prepared to count. + * + * @return Query + */ + public function getCountQuery() + { /* @var $countQuery Query */ $countQuery = $this->cloneQuery($this->query); @@ -153,10 +159,10 @@ class Paginator implements \Countable, \IteratorAggregate $countQuery->setFirstResult(null)->setMaxResults(null); $parser = new Query\Parser($countQuery); - $parameterMappings = $parser->parse($parser)->getParameterMappings(); + $parameterMappings = $parser->parse()->getParameterMappings(); $parameters = $countQuery->getParameters(); - foreach($parameters as $k=>$param){ - if(!array_key_exists($param->getName(), $parameterMappings)){ + foreach ($parameters as $k => $param){ + if( ! array_key_exists($param->getName(), $parameterMappings)) { $parameters->remove($k); } } From a80117a4eea4f468433f4e0c5e5a46475570db41 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 27 Nov 2014 17:42:14 +0100 Subject: [PATCH 07/13] #1188 - removing unused variable assignments --- lib/Doctrine/ORM/Tools/Pagination/Paginator.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index dc4801a46..056520260 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -118,12 +118,8 @@ class Paginator implements \Countable, \IteratorAggregate public function count() { if ($this->count === null) { - $countQuery = $this->getCountQuery(); - try { - $data = $countQuery->getScalarResult(); - $data = array_map('current', $data); - $this->count = array_sum($data); + $this->count = array_sum(array_map('current', $this->getCountQuery()->getScalarResult())); } catch(NoResultException $e) { $this->count = 0; } From 69064ac13dfbd8ca00e1c700f937166db406a50c Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 27 Nov 2014 17:48:37 +0100 Subject: [PATCH 08/13] #1188 - making `Doctrine\ORM\Tools\Pagination\Paginator#getCountQuery()` private, as it is an implementation detail --- .../ORM/Tools/Pagination/Paginator.php | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index 056520260..cbc5cc785 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -127,44 +127,6 @@ class Paginator implements \Countable, \IteratorAggregate return $this->count; } - /** - * Returns Query prepared to count. - * - * @return Query - */ - public function getCountQuery() - { - /* @var $countQuery Query */ - $countQuery = $this->cloneQuery($this->query); - - if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) { - $countQuery->setHint(CountWalker::HINT_DISTINCT, true); - } - - 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'); - } - - $countQuery->setFirstResult(null)->setMaxResults(null); - $parser = new Query\Parser($countQuery); - $parameterMappings = $parser->parse()->getParameterMappings(); - $parameters = $countQuery->getParameters(); - foreach ($parameters as $k => $param){ - if( ! array_key_exists($param->getName(), $parameterMappings)) { - $parameters->remove($k); - } - } - $countQuery->setParameters($parameters); - return $countQuery; - } /** * {@inheritdoc} @@ -267,4 +229,44 @@ class Paginator implements \Countable, \IteratorAggregate $hints[] = $walkerClass; $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints); } + + /** + * Returns Query prepared to count. + * + * @return Query + */ + private function getCountQuery() + { + /* @var $countQuery Query */ + $countQuery = $this->cloneQuery($this->query); + + if ( ! $countQuery->hasHint(CountWalker::HINT_DISTINCT)) { + $countQuery->setHint(CountWalker::HINT_DISTINCT, true); + } + + 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'); + } + + $countQuery->setFirstResult(null)->setMaxResults(null); + + $parser = new Query\Parser($countQuery); + $parameterMappings = $parser->parse()->getParameterMappings(); + $parameters = $countQuery->getParameters(); + foreach ($parameters as $k => $param){ + if( ! array_key_exists($param->getName(), $parameterMappings)) { + $parameters->remove($k); + } + } + $countQuery->setParameters($parameters); + return $countQuery; + } } From ecb09b5627498a0e6ad579ad40a056a967ba7f75 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 27 Nov 2014 17:50:27 +0100 Subject: [PATCH 09/13] #1188 - accessing `Doctrine\ORM\Tools\Pagination\Paginator#getCountQuery()` via reflection for test purposes --- .../Tests/ORM/Functional/PaginationTest.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index fd9b2d08d..96fabda04 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -11,6 +11,7 @@ use Doctrine\Tests\Models\CMS\CmsGroup; use Doctrine\Tests\Models\CMS\CmsArticle; use Doctrine\Tests\Models\CMS\CmsComment; use Doctrine\ORM\Tools\Pagination\Paginator; +use ReflectionMethod; /** * @group DDC-1613 @@ -151,7 +152,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(1, $paginator->count()); } - public function testCountQuery_ParametersInSelect() + public function testCountQueryStripsParametersInSelect() { /** @var $query Query */ $query = $this->_em->createQuery( @@ -162,13 +163,23 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $query->setFirstResult(null)->setMaxResults(null); $paginator = new Paginator($query); - $countQuery = $paginator->getCountQuery(); + + // + $getCountQuery = new ReflectionMethod($paginator, 'getCountQuery'); + + $getCountQuery->setAccessible(true); + + $countQuery = $getCountQuery->invoke($paginator); + $this->assertEquals(2, count($countQuery->getParameters())); $this->assertEquals(3, $paginator->count()); $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker'); + $paginator = new Paginator($query); - $countQuery = $paginator->getCountQuery(); + + $countQuery = $getCountQuery->invoke($paginator); + //if select part of query is replaced with count(...) paginator should remove parameters from query object not used in new query. $this->assertEquals(1, count($countQuery->getParameters())); $this->assertEquals(3, $paginator->count()); From 42547234d26bf35d2001e0288ed800386ad79541 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 27 Nov 2014 17:54:02 +0100 Subject: [PATCH 10/13] #1188 - minor CS fixes (avoiding DQL one-liner) --- tests/Doctrine/Tests/ORM/Functional/PaginationTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index 96fabda04..cf2cf3d75 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -156,7 +156,9 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase { /** @var $query Query */ $query = $this->_em->createQuery( - 'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < :id or 1=1 ORDER BY promotedFirst' + 'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst + FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u + WHERE u.id < :id or 1=1' ); $query->setParameter('vipMaxId', 10); $query->setParameter('id', 100); @@ -164,7 +166,6 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $paginator = new Paginator($query); - // $getCountQuery = new ReflectionMethod($paginator, 'getCountQuery'); $getCountQuery->setAccessible(true); From 64752532dd958647bdaf164666fffddf1bf8ad08 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 27 Nov 2014 17:57:42 +0100 Subject: [PATCH 11/13] #1188 - `assertCount` instead of `assertEquals` --- .../Tests/ORM/Functional/PaginationTest.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index cf2cf3d75..303bf4fe8 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -154,7 +154,6 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase public function testCountQueryStripsParametersInSelect() { - /** @var $query Query */ $query = $this->_em->createQuery( 'SELECT u, (CASE WHEN u.id < :vipMaxId THEN 1 ELSE 0 END) AS hidden promotedFirst FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u @@ -170,20 +169,17 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $getCountQuery->setAccessible(true); - $countQuery = $getCountQuery->invoke($paginator); - - $this->assertEquals(2, count($countQuery->getParameters())); - $this->assertEquals(3, $paginator->count()); + $this->assertCount(2, $getCountQuery->invoke($paginator)->getParameters()); + $this->assertCount(3, $paginator); $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker'); $paginator = new Paginator($query); - $countQuery = $getCountQuery->invoke($paginator); - - //if select part of query is replaced with count(...) paginator should remove parameters from query object not used in new query. - $this->assertEquals(1, count($countQuery->getParameters())); - $this->assertEquals(3, $paginator->count()); + // if select part of query is replaced with count(...) paginator should remove + // parameters from query object not used in new query. + $this->assertCount(1, $getCountQuery->invoke($paginator)->getParameters()); + $this->assertCount(3, $paginator); } public function populate() From ac0f1b2ce2e9389d5a627bef5016c6b99db9ec62 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 27 Nov 2014 17:59:12 +0100 Subject: [PATCH 12/13] #1188 - Importing parser class --- lib/Doctrine/ORM/Tools/Pagination/Paginator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index cbc5cc785..44082755d 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -19,6 +19,7 @@ namespace Doctrine\ORM\Tools\Pagination; +use Doctrine\ORM\Query\Parser; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Query; use Doctrine\ORM\Query\ResultSetMapping; @@ -258,7 +259,7 @@ class Paginator implements \Countable, \IteratorAggregate $countQuery->setFirstResult(null)->setMaxResults(null); - $parser = new Query\Parser($countQuery); + $parser = new Parser($countQuery); $parameterMappings = $parser->parse()->getParameterMappings(); $parameters = $countQuery->getParameters(); foreach ($parameters as $k => $param){ From f536daa1e419101a911571205db3af1214110d22 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 27 Nov 2014 18:08:11 +0100 Subject: [PATCH 13/13] #1188 - Simplified and optimized parameter un-setting logic --- lib/Doctrine/ORM/Tools/Pagination/Paginator.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php index 44082755d..c5b0b3686 100755 --- a/lib/Doctrine/ORM/Tools/Pagination/Paginator.php +++ b/lib/Doctrine/ORM/Tools/Pagination/Paginator.php @@ -261,13 +261,19 @@ class Paginator implements \Countable, \IteratorAggregate $parser = new Parser($countQuery); $parameterMappings = $parser->parse()->getParameterMappings(); - $parameters = $countQuery->getParameters(); - foreach ($parameters as $k => $param){ - if( ! array_key_exists($param->getName(), $parameterMappings)) { - $parameters->remove($k); + /* @var $parameters \Doctrine\Common\Collections\Collection|\Doctrine\ORM\Query\Parameter[] */ + $parameters = $countQuery->getParameters(); + + foreach ($parameters as $key => $parameter) { + $parameterName = $parameter->getName(); + + if( ! (isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings))) { + unset($parameters[$key]); } } + $countQuery->setParameters($parameters); + return $countQuery; } }