From 35a62e9a05a85ab7b2df5a5bee7b63a517dd76df Mon Sep 17 00:00:00 2001 From: Sander Marechal Date: Fri, 1 Nov 2013 13:43:03 +0100 Subject: [PATCH] Add rootAlias to Criteria where clauses --- .../ORM/Query/QueryExpressionVisitor.php | 28 ++++++++++++------- lib/Doctrine/ORM/QueryBuilder.php | 4 +-- .../ORM/Query/QueryExpressionVisitorTest.php | 28 +++++++++---------- tests/Doctrine/Tests/ORM/QueryBuilderTest.php | 2 +- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php b/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php index 2da6fb412..e4af98a94 100644 --- a/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php +++ b/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php @@ -47,6 +47,11 @@ class QueryExpressionVisitor extends ExpressionVisitor Comparison::LTE => Expr\Comparison::LTE ); + /** + * @var string + */ + private $rootAlias; + /** * @var Expr */ @@ -58,10 +63,13 @@ class QueryExpressionVisitor extends ExpressionVisitor private $parameters = array(); /** - * Constructor with internal initialization. + * Constructor + * + * @param string $rootAlias */ - public function __construct() + public function __construct($rootAlias) { + $this->rootAlias = $rootAlias; $this->expr = new Expr(); } @@ -133,38 +141,38 @@ class QueryExpressionVisitor extends ExpressionVisitor switch ($comparison->getOperator()) { case Comparison::IN: $this->parameters[] = $parameter; - return $this->expr->in($comparison->getField(), $placeholder); + return $this->expr->in($this->rootAlias . '.' . $comparison->getField(), $placeholder); case Comparison::NIN: $this->parameters[] = $parameter; - return $this->expr->notIn($comparison->getField(), $placeholder); + return $this->expr->notIn($this->rootAlias . '.' . $comparison->getField(), $placeholder); case Comparison::EQ: case Comparison::IS: if ($this->walkValue($comparison->getValue()) === null) { - return $this->expr->isNull($comparison->getField()); + return $this->expr->isNull($this->rootAlias . '.' . $comparison->getField()); } $this->parameters[] = $parameter; - return $this->expr->eq($comparison->getField(), $placeholder); + return $this->expr->eq($this->rootAlias . '.' . $comparison->getField(), $placeholder); case Comparison::NEQ: if ($this->walkValue($comparison->getValue()) === null) { - return $this->expr->isNotNull($comparison->getField()); + return $this->expr->isNotNull($this->rootAlias . '.' . $comparison->getField()); } $this->parameters[] = $parameter; - return $this->expr->neq($comparison->getField(), $placeholder); + return $this->expr->neq($this->rootAlias . '.' . $comparison->getField(), $placeholder); case Comparison::CONTAINS: $parameter->setValue('%' . $parameter->getValue() . '%', $parameter->getType()); $this->parameters[] = $parameter; - return $this->expr->like($comparison->getField(), $placeholder); + return $this->expr->like($this->rootAlias . '.' . $comparison->getField(), $placeholder); default: $operator = self::convertComparisonOperator($comparison->getOperator()); if ($operator) { $this->parameters[] = $parameter; return new Expr\Comparison( - $comparison->getField(), + $this->rootAlias . '.' . $comparison->getField(), $operator, $placeholder ); diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index b2b7cf37f..dba95aa5e 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -1087,7 +1087,8 @@ class QueryBuilder */ public function addCriteria(Criteria $criteria) { - $visitor = new QueryExpressionVisitor(); + $rootAlias = $this->getRootAlias(); + $visitor = new QueryExpressionVisitor($rootAlias); if ($whereExpression = $criteria->getWhereExpression()) { $this->andWhere($visitor->dispatch($whereExpression)); @@ -1096,7 +1097,6 @@ class QueryBuilder } } - $rootAlias = $this->getRootAlias(); if ($criteria->getOrderings()) { foreach ($criteria->getOrderings() as $sort => $order) { $this->addOrderBy($rootAlias . '.' . $sort, $order); diff --git a/tests/Doctrine/Tests/ORM/Query/QueryExpressionVisitorTest.php b/tests/Doctrine/Tests/ORM/Query/QueryExpressionVisitorTest.php index 5c7a5b776..1761da043 100644 --- a/tests/Doctrine/Tests/ORM/Query/QueryExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/ORM/Query/QueryExpressionVisitorTest.php @@ -47,7 +47,7 @@ class QueryExpressionVisitorTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - $this->visitor = new QueryExpressionVisitor(); + $this->visitor = new QueryExpressionVisitor('o'); } /** @@ -71,24 +71,24 @@ class QueryExpressionVisitorTest extends \PHPUnit_Framework_TestCase $qb = new QueryBuilder(); return array( - array($cb->eq('field', 'value'), $qb->eq('field', ':field'), new Parameter('field', 'value')), - array($cb->neq('field', 'value'), $qb->neq('field', ':field'), new Parameter('field', 'value')), - array($cb->eq('field', null), $qb->isNull('field')), - array($cb->neq('field', null), $qb->isNotNull('field')), - array($cb->isNull('field'), $qb->isNull('field')), + array($cb->eq('field', 'value'), $qb->eq('o.field', ':field'), new Parameter('field', 'value')), + array($cb->neq('field', 'value'), $qb->neq('o.field', ':field'), new Parameter('field', 'value')), + array($cb->eq('field', null), $qb->isNull('o.field')), + array($cb->neq('field', null), $qb->isNotNull('o.field')), + array($cb->isNull('field'), $qb->isNull('o.field')), - array($cb->gt('field', 'value'), $qb->gt('field', ':field'), new Parameter('field', 'value')), - array($cb->gte('field', 'value'), $qb->gte('field', ':field'), new Parameter('field', 'value')), - array($cb->lt('field', 'value'), $qb->lt('field', ':field'), new Parameter('field', 'value')), - array($cb->lte('field', 'value'), $qb->lte('field', ':field'), new Parameter('field', 'value')), + array($cb->gt('field', 'value'), $qb->gt('o.field', ':field'), new Parameter('field', 'value')), + array($cb->gte('field', 'value'), $qb->gte('o.field', ':field'), new Parameter('field', 'value')), + array($cb->lt('field', 'value'), $qb->lt('o.field', ':field'), new Parameter('field', 'value')), + array($cb->lte('field', 'value'), $qb->lte('o.field', ':field'), new Parameter('field', 'value')), - array($cb->in('field', array('value')), $qb->in('field', ':field'), new Parameter('field', array('value'))), - array($cb->notIn('field', array('value')), $qb->notIn('field', ':field'), new Parameter('field', array('value'))), + array($cb->in('field', array('value')), $qb->in('o.field', ':field'), new Parameter('field', array('value'))), + array($cb->notIn('field', array('value')), $qb->notIn('o.field', ':field'), new Parameter('field', array('value'))), - array($cb->contains('field', 'value'), $qb->like('field', ':field'), new Parameter('field', '%value%')), + array($cb->contains('field', 'value'), $qb->like('o.field', ':field'), new Parameter('field', '%value%')), // Test parameter conversion - array($cb->eq('object.field', 'value'), $qb->eq('object.field', ':object_field'), new Parameter('object_field', 'value')), + array($cb->eq('object.field', 'value'), $qb->eq('o.object.field', ':object_field'), new Parameter('object_field', 'value')), ); } diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index ab9c7bd85..6b1936a83 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -410,7 +410,7 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase $qb->addCriteria($criteria); - $this->assertEquals('field = :field', (string) $qb->getDQLPart('where')); + $this->assertEquals('u.field = :field', (string) $qb->getDQLPart('where')); $this->assertNotNull($qb->getParameter('field')); }