DDC-55: Query paramater mapping error #66

Closed
opened 2026-01-22 12:25:51 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 17, 2009).

Jira issue originally created by user mcurcio:

It seems as though the query parameters start from '1' (?0 throws an exception), whereas the Query#_prepareParams function starts counting from 0.

Code:
$query = $this->em->createQuery('SELECT s FROM Session s WHERE s.id = ?1');

echo $query->getSQL();

$session = $query->execute(array($id));
print_r($session);

Output:
SELECT s0_.id AS id0, s0_.data AS data1, s0_.created AS created2, s0_.accessed AS accessed3 FROM Session s0_ WHERE s0_.id = ?

Warning: Invalid argument supplied for foreach() in Doctrine/ORM/Query.php on line 222

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1' in Doctrine/DBAL/Connection.php:602
Stack trace:
#0 Doctrine/DBAL/Connection.php(602): PDO->query('SELECT s0_.id A...')
#1 Doctrine/ORM/Query/Exec/SingleSelectExecutor.php(42): Doctrine\DBAL\Connection->execute('SELECT s0_.id A...', Array)
#2 Doctrine/ORM/Query.php(198): Doctrine\ORM\Query\Exec\SingleSelectExecutor->execute(Object(Doctrine\DBAL\Connection), Array)
#3 Doctrine/ORM/AbstractQuery.php(461): Doctrine\ORM\Query->_doExecute(Array)
#4 classes/SessionManager.php(35): Doctrine\ORM\AbstractQuery->execute(A in /var/www/tec-expo.com/development/libraries/Doctrine/DBAL/Connection.php on line 602

Originally created by @doctrinebot on GitHub (Oct 17, 2009). Jira issue originally created by user mcurcio: It seems as though the query parameters start from '1' (?0 throws an exception), whereas the Query#_prepareParams function starts counting from 0. Code: $query = $this->em->createQuery('SELECT s FROM Session s WHERE s.id = ?1'); echo $query->getSQL(); $session = $query->execute(array($id)); print_r($session); Output: SELECT s0_.id AS id0, s0_.data AS data1, s0_.created AS created2, s0_.accessed AS accessed3 FROM Session s0_ WHERE s0_.id = ? Warning: Invalid argument supplied for foreach() in Doctrine/ORM/Query.php on line 222 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1' in Doctrine/DBAL/Connection.php:602 Stack trace: #0 Doctrine/DBAL/Connection.php(602): PDO->query('SELECT s0_.id A...') #1 Doctrine/ORM/Query/Exec/SingleSelectExecutor.php(42): Doctrine\DBAL\Connection->execute('SELECT s0_.id A...', Array) #2 Doctrine/ORM/Query.php(198): Doctrine\ORM\Query\Exec\SingleSelectExecutor->execute(Object(Doctrine\DBAL\Connection), Array) #3 Doctrine/ORM/AbstractQuery.php(461): Doctrine\ORM\Query->_doExecute(Array) #4 classes/SessionManager.php(35): Doctrine\ORM\AbstractQuery->execute(A in /var/www/tec-expo.com/development/libraries/Doctrine/DBAL/Connection.php on line 602
admin added the Bug label 2026-01-22 12:25:51 +01:00
admin closed this issue 2026-01-22 12:25:52 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 17, 2009):

Comment created by romanb:

This:

$query->execute(array($id));

is the same as this:

$query->execute(array(0 => $id));

is the same as this:

$query->setParameter(0, $id);
$query->execute();

Hence the error. We should probably check for isset($params[0]) in execute() and throw an exception in this case.

To get it to work, either use:

$query->execute(array(1 => $id));

or

$query->setParameter(1, $id);
$query->execute(); // or $query->getResult()
@doctrinebot commented on GitHub (Oct 17, 2009): Comment created by romanb: This: ``` $query->execute(array($id)); ``` is the same as this: ``` $query->execute(array(0 => $id)); ``` is the same as this: ``` $query->setParameter(0, $id); $query->execute(); ``` Hence the error. We should probably check for isset($params[0]) in execute() and throw an exception in this case. To get it to work, either use: ``` $query->execute(array(1 => $id)); ``` or ``` $query->setParameter(1, $id); $query->execute(); // or $query->getResult() ```
Author
Owner

@doctrinebot commented on GitHub (Oct 19, 2009):

Comment created by mcurcio:

While I have since discovered the "colon-mapping" scheme, for the users who would prefer this mapping method, why not allow the zero position? Starting the count from zero would allow the simple execute() mechanism that I was trying, and while not very clear, might be useful for certain cases.

@doctrinebot commented on GitHub (Oct 19, 2009): Comment created by mcurcio: While I have since discovered the "colon-mapping" scheme, for the users who would prefer this mapping method, why not allow the zero position? Starting the count from zero would allow the simple execute() mechanism that I was trying, and while not very clear, might be useful for certain cases.
Author
Owner

@doctrinebot commented on GitHub (Oct 28, 2009):

Comment created by romanb:

Fixed now. A meaninfgul exception is thrown.

Starting from 0 would be unintuitive as even PDO starts to count from 1.

@doctrinebot commented on GitHub (Oct 28, 2009): Comment created by romanb: Fixed now. A meaninfgul exception is thrown. Starting from 0 would be unintuitive as even PDO starts to count from 1.
Author
Owner

@doctrinebot commented on GitHub (Oct 28, 2009):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Oct 28, 2009): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 1 attachments from Jira into https://gist.github.com/795011b7186bd236bcb0

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 1 attachments from Jira into https://gist.github.com/795011b7186bd236bcb0 - [10077_param_mapping.diff](https://gist.github.com/795011b7186bd236bcb0#file-10077_param_mapping-diff)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#66