From 54a0109d5d644536be427ff96e347e1a3cad8840 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Wed, 11 May 2011 17:30:42 -0300 Subject: [PATCH] [DDC-1147] Allowed usage of 0-based input parameters in DQL. --- lib/Doctrine/ORM/AbstractQuery.php | 8 ++------ lib/Doctrine/ORM/Query/Lexer.php | 2 +- lib/Doctrine/ORM/Query/SqlWalker.php | 2 +- .../Doctrine/Tests/ORM/Functional/QueryTest.php | 17 +++++++++++++++++ tests/Doctrine/Tests/ORM/Query/QueryTest.php | 9 --------- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index 787770a34..5ed1ef9e5 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -558,10 +558,6 @@ abstract class AbstractQuery $this->setParameters($params); } - if (isset($this->_params[0])) { - throw QueryException::invalidParameterPosition(0); - } - // Check result cache if ($this->_useResultCache && $cacheDriver = $this->getResultCacheDriver()) { list($key, $hash) = $this->getResultCacheId(); @@ -572,8 +568,8 @@ abstract class AbstractQuery $stmt = $this->_doExecute(); $result = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll( - $stmt, $this->_resultSetMapping, $this->_hints - ); + $stmt, $this->_resultSetMapping, $this->_hints + ); $cacheDriver->save($hash, array($key => $result), $this->_resultCacheTTL); diff --git a/lib/Doctrine/ORM/Query/Lexer.php b/lib/Doctrine/ORM/Query/Lexer.php index 82355faec..70c8b7db6 100644 --- a/lib/Doctrine/ORM/Query/Lexer.php +++ b/lib/Doctrine/ORM/Query/Lexer.php @@ -126,7 +126,7 @@ class Lexer extends \Doctrine\Common\Lexer '[a-z_\\\][a-z0-9_\:\\\]*[a-z0-9_]{1}', '(?:[0-9]+(?:[\.][0-9]+)*)(?:e[+-]?[0-9]+)?', "'(?:[^']|'')*'", - '\?[1-9][0-9]*|:[a-z]{1}[a-z0-9_]{0,}' + '\?[0-9]*|:[a-z]{1}[a-z0-9_]{0,}' ); } diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index a14987eaa..0b5cfbef0 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -44,7 +44,7 @@ class SqlWalker implements TreeWalker private $_aliasCounter = 0; private $_tableAliasCounter = 0; private $_scalarResultCounter = 1; - private $_sqlParamIndex = 1; + private $_sqlParamIndex = 0; /** * @var ParserResult diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 02b4d267e..323d0e88f 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -95,6 +95,23 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals('Symfony 2', $users[0]->articles[1]->topic); } + public function testUsingZeroBasedQueryParameterShouldWork() + { + $user = new CmsUser; + $user->name = 'Jonathan'; + $user->username = 'jwage'; + $user->status = 'developer'; + $this->_em->persist($user); + $this->_em->flush(); + $this->_em->clear(); + + $q = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?0'); + $q->setParameter(0, 'jwage'); + $user = $q->getSingleResult(); + + $this->assertNotNull($user); + } + public function testUsingUnknownQueryParameterShouldThrowException() { $this->setExpectedException( diff --git a/tests/Doctrine/Tests/ORM/Query/QueryTest.php b/tests/Doctrine/Tests/ORM/Query/QueryTest.php index c5f36f387..b1d2bee23 100644 --- a/tests/Doctrine/Tests/ORM/Query/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Query/QueryTest.php @@ -13,15 +13,6 @@ class QueryTest extends \Doctrine\Tests\OrmTestCase $this->_em = $this->_getTestEntityManager(); } - /** - * @expectedException Doctrine\ORM\Query\QueryException - */ - public function testParameterIndexZeroThrowsException() - { - $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); - $query->execute(array(42)); // same as array(0 => 42), 0 is invalid parameter position - } - public function testGetParameters() { $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");