diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index bcea7c000..27f65e14c 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -888,6 +888,40 @@ class QueryBuilder . (isset($options['post']) ? $options['post'] : ''); } + /** + * Reset DQL parts + * + * @param array $parts + * @return QueryBuilder + */ + public function resetDQLParts($parts = null) + { + if (is_null($parts)) { + $parts = array_keys($this->_dqlParts); + } + foreach ($parts as $part) { + $this->resetDQLPart($part); + } + return $this; + } + + /** + * Reset single DQL part + * + * @param string $part + * @return QueryBuilder; + */ + public function resetDQLPart($part) + { + if (is_array($this->_dqlParts[$part])) { + $this->_dqlParts[$part] = array(); + } else { + $this->_dqlParts[$part] = null; + } + $this->_state = self::STATE_DIRTY; + return $this; + } + /** * Gets a string representation of this QueryBuilder which corresponds to * the final DQL query being constructed. diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index e2430e8cc..1ad039bb9 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -525,4 +525,48 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase $this->assertValidQueryBuilder($qb, 'SELECT COUNT(e.id)'); } + + public function testResetDQLPart() + { + $qb = $this->_em->createQueryBuilder() + ->select('u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') + ->where('u.username = ?1')->orderBy('u.username'); + + $this->assertEquals('u.username = ?1', (string)$qb->getDQLPart('where')); + $this->assertEquals(1, count($qb->getDQLPart('orderBy'))); + + $qb->resetDqlPart('where')->resetDqlPart('orderBy'); + + $this->assertNull($qb->getDQLPart('where')); + $this->assertEquals(0, count($qb->getDQLPart('orderBy'))); + } + + public function testResetDQLParts() + { + $qb = $this->_em->createQueryBuilder() + ->select('u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') + ->where('u.username = ?1')->orderBy('u.username'); + + $qb->resetDQLParts(array('where', 'orderBy')); + + $this->assertEquals(1, count($qb->getDQLPart('select'))); + $this->assertNull($qb->getDQLPart('where')); + $this->assertEquals(0, count($qb->getDQLPart('orderBy'))); + } + + public function testResetAllDQLParts() + { + $qb = $this->_em->createQueryBuilder() + ->select('u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') + ->where('u.username = ?1')->orderBy('u.username'); + + $qb->resetDQLParts(); + + $this->assertEquals(0, count($qb->getDQLPart('select'))); + $this->assertNull($qb->getDQLPart('where')); + $this->assertEquals(0, count($qb->getDQLPart('orderBy'))); + } } \ No newline at end of file