diff --git a/core/lib/Drupal/Core/Database/Query/Condition.php b/core/lib/Drupal/Core/Database/Query/Condition.php index 784ed78..c23ad76 100644 --- a/core/lib/Drupal/Core/Database/Query/Condition.php +++ b/core/lib/Drupal/Core/Database/Query/Condition.php @@ -259,8 +259,13 @@ public function __toString() { function __clone() { $this->changed = TRUE; foreach ($this->conditions as $key => $condition) { - if ($key !== '#conjunction' && $condition['field'] instanceOf ConditionInterface) { - $this->conditions[$key]['field'] = clone($condition['field']); + if ($key !== '#conjunction') { + if ($condition['field'] instanceOf ConditionInterface) { + $this->conditions[$key]['field'] = clone($condition['field']); + } + if ($condition['value'] instanceOf SelectInterface) { + $this->conditions[$key]['value'] = clone($condition['value']); + } } } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/SelectCloneTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/SelectCloneTest.php new file mode 100644 index 0000000..8052ac2 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Database/SelectCloneTest.php @@ -0,0 +1,51 @@ + 'Select tests, cloning', + 'description' => 'Test cloning Select queries.', + 'group' => 'Database', + ); + } + + /** + * Test that subqueries as value within conditions are cloned properly. + */ + function testSelectConditionSubQueryCloning() { + $subquery = db_select('test', 't'); + $subquery->addField('t', 'id', 'id'); + $subquery->condition('age', 28, '<'); + + $query = db_select('test', 't'); + $query->addField('t', 'name', 'name'); + $query->condition('id', $subquery, 'IN'); + + $clone = clone $query; + $result = $clone->execute(); + $num_records = 0; + foreach ($result as $record) { + $num_records++; + } + $this->assertEqual(3, $num_records, 'The cloned query returns the expected number of rows'); + + $query->condition('age', 25, '>'); + $result = $query->execute(); + $num_records = 0; + foreach ($result as $record) { + $num_records++; + } + $this->assertEqual(2, $num_records, 'The modified query returns the expected number of rows'); + } +} \ No newline at end of file