diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index 27f65e14c..436522b69 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -932,4 +932,24 @@ class QueryBuilder { return $this->getDQL(); } + + /** + * Deep clone of all expression objects in the DQL parts. + * + * @return void + */ + public function __clone() + { + foreach ($this->_dqlParts AS $part => $elements) { + if (is_array($this->_dqlParts[$part])) { + foreach ($this->_dqlParts[$part] AS $idx => $element) { + if (is_object($element)) { + $this->_dqlParts[$part][$idx] = clone $element; + } + } + } else if (\is_object($elements)) { + $this->_dqlParts[$part] = clone $elements; + } + } + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php index 1ad039bb9..f18516fc4 100644 --- a/tests/Doctrine/Tests/ORM/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/QueryBuilderTest.php @@ -569,4 +569,24 @@ class QueryBuilderTest extends \Doctrine\Tests\OrmTestCase $this->assertNull($qb->getDQLPart('where')); $this->assertEquals(0, count($qb->getDQLPart('orderBy'))); } + + /** + * @group DDC-867 + */ + public function testDeepClone() + { + $qb = $this->_em->createQueryBuilder() + ->select('u') + ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u') + ->andWhere('u.username = ?1') + ->andWhere('u.status = ?2'); + + $expr = $qb->getDQLPart('where'); + $this->assertEquals(2, $expr->count(), "Modifying the second query should affect the first one."); + + $qb2 = clone $qb; + $qb2->andWhere('u.name = ?3'); + + $this->assertEquals(2, $expr->count(), "Modifying the second query should affect the first one."); + } } \ No newline at end of file