Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Fix expressionParamIndex for AbstractSql #2416

wants to merge 1 commit into


None yet
3 participants

jdolieslager commented Sep 25, 2012

I experience some problems with creating joins in combination with Predicates. The predicate is build as identifier = value. When preparing this statement the prepared statement has three times joins1. The expected result is join1, join2, join3.

The fix is really simple. For a given AbstractSql instance keep track of the last index for the provided $namedParameterPrefix. When it does not exists add it with index=1, otherwise use the given index.

The fix will be general. So every namedParameterPrefix will be tracked and corrected if needed.

Code for reproducing
$predicateA = new \Zend\Db\Sql\Predicate\Predicate();
$predicateA->equalTo('id', 1);

$predicateB = new \Zend\Db\Sql\Predicate\Predicate();
$predicateB->equalTo('id', 2);

$predicateC = new \Zend\Db\Sql\Predicate\Predicate();
$predicateC->equalTo('id', 3);

$sql = new \Zend\Db\Sql\Sql($this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'));

$select = new \Zend\Db\Sql\Select();
    ->join('tableA', $predicateA)
    ->join('tableB', $predicateB)
    ->join('tableC', $predicateC);

$stmt = $sql->prepareStatementForSqlObject($select);
echo $stmt->getSql();
The result
SELECT `main`.*, `tableA`.*, `tableB`.*, `tableC`.* FROM `main` INNER JOIN `tableA` ON `id` = :join1 INNER JOIN `tableB` ON `id` = :join1 INNER JOIN `tableC` ON `id` = :join1
Expected Result
SELECT `main`.*, `tableA`.*, `tableB`.*, `tableC`.* FROM `main` INNER JOIN `tableA` ON `id` = :join1 INNER JOIN `tableB` ON `id` = :join2 INNER JOIN `tableC` ON `id` = :join3

@ghost ghost assigned ralphschindler Sep 25, 2012


ralphschindler commented Sep 25, 2012

Ah, I see the problem. Ultimately, it's the responsibility of the Select object to give the abstract expression processor the right prefix. I know how to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment