Skip to content

Commit

Permalink
[DDC-1237] Fixed issue with QueryBuilder where user may have includes…
Browse files Browse the repository at this point in the history
… nested complex expression in a string format while consuming a composite expression (AND or OR).
  • Loading branch information
guilhermeblanco committed Jul 3, 2011
1 parent ffca455 commit 550fcbc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/Doctrine/ORM/Query/Expr/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,26 @@ public function __toString()
$components = array();

foreach ($this->_parts as $part) {
$components[] = (is_object($part) && $part instanceof self && $part->count() > 1)
? $this->_preSeparator . ((string) $part) . $this->_postSeparator
: ((string) $part);
$components[] = $this->processQueryPart($part);
}

return implode($this->_separator, $components);
}


private function processQueryPart($part)
{
$queryPart = (string) $part;

if (is_object($part) && $part instanceof self && $part->count() > 1) {
return $this->_preSeparator . $queryPart . $this->_postSeparator;
}

// Fixes DDC-1237: User may have added a where item containing nested expression (with "OR" or "AND")
if (mb_stripos($queryPart, ' OR ') !== false || mb_stripos($queryPart, ' AND ') !== false) {
return $this->_preSeparator . $queryPart . $this->_postSeparator;
}

return $queryPart;
}
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ public function testWhere()

$this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :uid');
}

public function testComplexAndWhere()
{
$qb = $this->_em->createQueryBuilder()
->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->where('u.id = :uid OR u.id = :uid2 OR u.id = :uid3')
->andWhere('u.name = :name');

$this->assertValidQueryBuilder($qb, 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id = :uid OR u.id = :uid2 OR u.id = :uid3) AND u.name = :name');
}

public function testAndWhere()
{
Expand Down

0 comments on commit 550fcbc

Please sign in to comment.