1
0
mirror of synced 2024-12-05 03:06:05 +03:00

added more informative error messages when invalid parameter count

This commit is contained in:
Rhodri Pugh 2014-07-15 13:17:22 +01:00
parent 85fbf68436
commit ad10a18071
3 changed files with 36 additions and 6 deletions

View File

@ -287,9 +287,13 @@ final class Query extends AbstractQuery
// Prepare parameters
$paramMappings = $this->_parserResult->getParameterMappings();
$paramCount = count($this->parameters);
$mappingCount = count($paramMappings);
if (count($paramMappings) != count($this->parameters)) {
throw QueryException::invalidParameterNumber();
if ($paramCount > $mappingCount) {
throw QueryException::tooManyParameters($mappingCount, $paramCount);
} elseif ($paramCount < $mappingCount) {
throw QueryException::tooFewParameters($mappingCount, $paramCount);
}
// evict all cache for the entity region

View File

@ -93,11 +93,25 @@ class QueryException extends \Doctrine\ORM\ORMException
}
/**
* @param integer $expected
* @param integer $received
*
* @return QueryException
*/
public static function invalidParameterNumber()
public static function tooManyParameters($expected, $received)
{
return new self("Invalid parameter number: number of bound variables does not match number of tokens");
return new self('Too many parameters: the query defines ' . $expected . ' parameters and you bound ' . $received);
}
/**
* @param integer $expected
* @param integer $received
*
* @return QueryException
*/
public static function tooFewParameters($expected, $received)
{
return new self('Too few parameters: the query defines ' . $expected . ' parameters but you only bound ' . $received);
}
/**

View File

@ -127,11 +127,11 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$user = $q->getSingleResult();
}
public function testMismatchingParamExpectedParamCount()
public function testTooManyParametersShouldThrowException()
{
$this->setExpectedException(
"Doctrine\ORM\Query\QueryException",
"Invalid parameter number: number of bound variables does not match number of tokens"
"Too many parameters: the query defines 1 parameters and you bound 2"
);
$q = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?1');
@ -141,6 +141,18 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$user = $q->getSingleResult();
}
public function testTooFewParametersShouldThrowException()
{
$this->setExpectedException(
"Doctrine\ORM\Query\QueryException",
"Too few parameters: the query defines 1 parameters but you only bound 0"
);
$q = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?1');
$user = $q->getSingleResult();
}
public function testInvalidInputParameterThrowsException()
{
$this->setExpectedException("Doctrine\ORM\Query\QueryException");