1
0
mirror of synced 2025-01-09 10:37:09 +03:00

Merge pull request #830 from sandermarechal/sort-alias

[DDC-2764] Prefix criteria orderBy with rootAlias
This commit is contained in:
Benjamin Eberlei 2014-01-02 14:15:01 -08:00
commit 96ec3c1613
4 changed files with 51 additions and 29 deletions

View File

@ -47,6 +47,11 @@ class QueryExpressionVisitor extends ExpressionVisitor
Comparison::LTE => Expr\Comparison::LTE Comparison::LTE => Expr\Comparison::LTE
); );
/**
* @var string
*/
private $rootAlias;
/** /**
* @var Expr * @var Expr
*/ */
@ -58,10 +63,13 @@ class QueryExpressionVisitor extends ExpressionVisitor
private $parameters = array(); 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(); $this->expr = new Expr();
} }
@ -141,38 +149,38 @@ class QueryExpressionVisitor extends ExpressionVisitor
switch ($comparison->getOperator()) { switch ($comparison->getOperator()) {
case Comparison::IN: case Comparison::IN:
$this->parameters[] = $parameter; $this->parameters[] = $parameter;
return $this->expr->in($comparison->getField(), $placeholder); return $this->expr->in($this->rootAlias . '.' . $comparison->getField(), $placeholder);
case Comparison::NIN: case Comparison::NIN:
$this->parameters[] = $parameter; $this->parameters[] = $parameter;
return $this->expr->notIn($comparison->getField(), $placeholder); return $this->expr->notIn($this->rootAlias . '.' . $comparison->getField(), $placeholder);
case Comparison::EQ: case Comparison::EQ:
case Comparison::IS: case Comparison::IS:
if ($this->walkValue($comparison->getValue()) === null) { if ($this->walkValue($comparison->getValue()) === null) {
return $this->expr->isNull($comparison->getField()); return $this->expr->isNull($this->rootAlias . '.' . $comparison->getField());
} }
$this->parameters[] = $parameter; $this->parameters[] = $parameter;
return $this->expr->eq($comparison->getField(), $placeholder); return $this->expr->eq($this->rootAlias . '.' . $comparison->getField(), $placeholder);
case Comparison::NEQ: case Comparison::NEQ:
if ($this->walkValue($comparison->getValue()) === null) { if ($this->walkValue($comparison->getValue()) === null) {
return $this->expr->isNotNull($comparison->getField()); return $this->expr->isNotNull($this->rootAlias . '.' . $comparison->getField());
} }
$this->parameters[] = $parameter; $this->parameters[] = $parameter;
return $this->expr->neq($comparison->getField(), $placeholder); return $this->expr->neq($this->rootAlias . '.' . $comparison->getField(), $placeholder);
case Comparison::CONTAINS: case Comparison::CONTAINS:
$parameter->setValue('%' . $parameter->getValue() . '%', $parameter->getType()); $parameter->setValue('%' . $parameter->getValue() . '%', $parameter->getType());
$this->parameters[] = $parameter; $this->parameters[] = $parameter;
return $this->expr->like($comparison->getField(), $placeholder); return $this->expr->like($this->rootAlias . '.' . $comparison->getField(), $placeholder);
default: default:
$operator = self::convertComparisonOperator($comparison->getOperator()); $operator = self::convertComparisonOperator($comparison->getOperator());
if ($operator) { if ($operator) {
$this->parameters[] = $parameter; $this->parameters[] = $parameter;
return new Expr\Comparison( return new Expr\Comparison(
$comparison->getField(), $this->rootAlias . '.' . $comparison->getField(),
$operator, $operator,
$placeholder $placeholder
); );

View File

@ -1087,7 +1087,8 @@ class QueryBuilder
*/ */
public function addCriteria(Criteria $criteria) public function addCriteria(Criteria $criteria)
{ {
$visitor = new QueryExpressionVisitor(); $rootAlias = $this->getRootAlias();
$visitor = new QueryExpressionVisitor($rootAlias);
if ($whereExpression = $criteria->getWhereExpression()) { if ($whereExpression = $criteria->getWhereExpression()) {
$this->andWhere($visitor->dispatch($whereExpression)); $this->andWhere($visitor->dispatch($whereExpression));
@ -1098,7 +1099,7 @@ class QueryBuilder
if ($criteria->getOrderings()) { if ($criteria->getOrderings()) {
foreach ($criteria->getOrderings() as $sort => $order) { foreach ($criteria->getOrderings() as $sort => $order) {
$this->addOrderBy($sort, $order); $this->addOrderBy($rootAlias . '.' . $sort, $order);
} }
} }

View File

@ -47,7 +47,7 @@ class QueryExpressionVisitorTest extends \PHPUnit_Framework_TestCase
*/ */
protected function setUp() 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(); $qb = new QueryBuilder();
return array( return array(
array($cb->eq('field', 'value'), $qb->eq('field', ':field'), new Parameter('field', 'value')), array($cb->eq('field', 'value'), $qb->eq('o.field', ':field'), new Parameter('field', 'value')),
array($cb->neq('field', 'value'), $qb->neq('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('field')), array($cb->eq('field', null), $qb->isNull('o.field')),
array($cb->neq('field', null), $qb->isNotNull('field')), array($cb->neq('field', null), $qb->isNotNull('o.field')),
array($cb->isNull('field'), $qb->isNull('field')), array($cb->isNull('field'), $qb->isNull('o.field')),
array($cb->gt('field', 'value'), $qb->gt('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('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('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('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->in('field', array('value')), $qb->in('o.field', ':field'), new Parameter('field', array('value'))),
array($cb->notIn('field', array('value')), $qb->notIn('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 // 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')),
); );
} }

View File

@ -402,12 +402,15 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
public function testAddCriteriaWhere() public function testAddCriteriaWhere()
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$criteria = new Criteria(); $criteria = new Criteria();
$criteria->where($criteria->expr()->eq('field', 'value')); $criteria->where($criteria->expr()->eq('field', 'value'));
$qb->addCriteria($criteria); $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')); $this->assertNotNull($qb->getParameter('field'));
} }
@ -493,18 +496,24 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
public function testAddCriteriaOrder() public function testAddCriteriaOrder()
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$criteria = new Criteria(); $criteria = new Criteria();
$criteria->orderBy(array('field' => Criteria::DESC)); $criteria->orderBy(array('field' => Criteria::DESC));
$qb->addCriteria($criteria); $qb->addCriteria($criteria);
$this->assertCount(1, $orderBy = $qb->getDQLPart('orderBy')); $this->assertCount(1, $orderBy = $qb->getDQLPart('orderBy'));
$this->assertEquals('field DESC', (string) $orderBy[0]); $this->assertEquals('u.field DESC', (string) $orderBy[0]);
} }
public function testAddCriteriaLimit() public function testAddCriteriaLimit()
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
$criteria = new Criteria(); $criteria = new Criteria();
$criteria->setFirstResult(2); $criteria->setFirstResult(2);
$criteria->setMaxResults(10); $criteria->setMaxResults(10);
@ -518,7 +527,11 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase
public function testAddCriteriaUndefinedLimit() public function testAddCriteriaUndefinedLimit()
{ {
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->setFirstResult(2)->setMaxResults(10); $qb->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->setFirstResult(2)
->setMaxResults(10);
$criteria = new Criteria(); $criteria = new Criteria();
$qb->addCriteria($criteria); $qb->addCriteria($criteria);