Commit 1c5d14b8 authored by Paul's avatar Paul

Added deep clone to QueryBuilder

parent da9564df
...@@ -1096,4 +1096,36 @@ class QueryBuilder ...@@ -1096,4 +1096,36 @@ class QueryBuilder
return $sql; return $sql;
} }
/**
* Deep clone of all expression objects in the SQL parts.
*
* @return void
*/
public function __clone()
{
foreach ($this->sqlParts as $part => $elements) {
if (is_array($this->sqlParts[$part])) {
foreach ($this->sqlParts[$part] as $idx => $element) {
if (is_object($element)) {
$this->sqlParts[$part][$idx] = clone $element;
}
}
} else if (is_object($elements)) {
$this->sqlParts[$part] = clone $elements;
}
}
$params = array();
foreach ($this->params as $param) {
if(is_object($param)){
$params[] = clone $param;
} else {
$params[] = $param;
}
}
$this->params = $params;
}
} }
...@@ -602,4 +602,24 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -602,4 +602,24 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals('SELECT DISTINCT u.id FROM users u INNER JOIN permissions p ON p.user_id = u.id, articles a INNER JOIN comments c ON c.article_id = a.id WHERE (u.id = a.user_id) AND (p.read = 1)', $qb->getSQL()); $this->assertEquals('SELECT DISTINCT u.id FROM users u INNER JOIN permissions p ON p.user_id = u.id, articles a INNER JOIN comments c ON c.article_id = a.id WHERE (u.id = a.user_id) AND (p.read = 1)', $qb->getSQL());
} }
public function testClone()
{
$qb = new QueryBuilder($this->conn);
$qb->select('u.id')
->from('users', 'u')
->where('u.id = :test');
$qb->setParameter(':test', (object) 1);
$qb_clone = clone $qb;
$this->assertEquals((string) $qb, (string) $qb_clone);
$qb->andWhere('u.id = 1');
$this->assertFalse($qb->getQueryParts() === $qb_clone->getQueryParts());
$this->assertFalse($qb->getParameters() === $qb_clone->getParameters());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment