Loading core/lib/Drupal/Core/Entity/Query/ConditionAggregateInterface.php +18 −0 Original line number Diff line number Diff line Loading @@ -92,4 +92,22 @@ public function &conditions(); */ public function compile($query); /** * Sets the query that holds this condition. * * @param \Drupal\Core\Entity\Query\QueryInterface $query * New query to use. * * @return static */ public function setQuery(QueryInterface $query): static; /** * Gets the query that is holding this condition. * * @return \Drupal\Core\Entity\Query\QueryInterface * The query object. */ public function getQuery(): QueryInterface; } core/lib/Drupal/Core/Entity/Query/QueryBase.php +4 −1 Original line number Diff line number Diff line Loading @@ -343,10 +343,13 @@ public function tableSort(&$headers) { } /** * Makes sure that the Condition object is cloned as well. * Makes sure that condition objects are cloned as well. */ public function __clone() { $this->condition = clone $this->condition; if (isset($this->conditionAggregate)) { $this->conditionAggregate = clone $this->conditionAggregate; } } /** Loading core/lib/Drupal/Core/Entity/Query/Sql/Query.php +4 −5 Original line number Diff line number Diff line Loading @@ -327,11 +327,7 @@ protected function isSimpleQuery() { } /** * Implements the magic __clone method. * * Reset SQL query and tables collected. * Reset fields and GROUP BY. * Ensure condition points to the new query. * Resets cached SQL query state and re-parents conditions on clone. */ public function __clone() { parent::__clone(); Loading @@ -340,6 +336,9 @@ public function __clone() { $this->sqlFields = []; $this->sqlGroupBy = []; $this->condition->setQuery($this); if (isset($this->conditionAggregate)) { $this->conditionAggregate->setQuery($this); } } /** Loading core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php +30 −0 Original line number Diff line number Diff line Loading @@ -662,6 +662,36 @@ public function testAlterHook(): void { ]); } /** * Tests that cloning an aggregate query isolates its aggregate conditions. */ public function testCloneAggregateIsolation(): void { // Original query: groups with id count > 1 are user 2 (3 rows) and user 3 // (2 rows). $original = $this->entityStorage->getAggregateQuery() ->accessCheck(FALSE) ->aggregate('id', 'count') ->groupBy('user_id') ->conditionAggregate('id', 'count', 1, '>'); $clone = clone $original; // Tightening the clone must not affect the original's conditions. $clone->conditionAggregate('id', 'count', 2, '>'); $this->queryResult = $original->execute(); $this->assertCount(2, $this->queryResult); $this->assertResults([ ['user_id' => 2, 'id_count' => 3], ['user_id' => 3, 'id_count' => 2], ]); $this->queryResult = $clone->execute(); $this->assertCount(1, $this->queryResult); $this->assertResults([ ['user_id' => 2, 'id_count' => 3], ]); } /** * Asserts the results as expected regardless of order between and in rows. * Loading Loading
core/lib/Drupal/Core/Entity/Query/ConditionAggregateInterface.php +18 −0 Original line number Diff line number Diff line Loading @@ -92,4 +92,22 @@ public function &conditions(); */ public function compile($query); /** * Sets the query that holds this condition. * * @param \Drupal\Core\Entity\Query\QueryInterface $query * New query to use. * * @return static */ public function setQuery(QueryInterface $query): static; /** * Gets the query that is holding this condition. * * @return \Drupal\Core\Entity\Query\QueryInterface * The query object. */ public function getQuery(): QueryInterface; }
core/lib/Drupal/Core/Entity/Query/QueryBase.php +4 −1 Original line number Diff line number Diff line Loading @@ -343,10 +343,13 @@ public function tableSort(&$headers) { } /** * Makes sure that the Condition object is cloned as well. * Makes sure that condition objects are cloned as well. */ public function __clone() { $this->condition = clone $this->condition; if (isset($this->conditionAggregate)) { $this->conditionAggregate = clone $this->conditionAggregate; } } /** Loading
core/lib/Drupal/Core/Entity/Query/Sql/Query.php +4 −5 Original line number Diff line number Diff line Loading @@ -327,11 +327,7 @@ protected function isSimpleQuery() { } /** * Implements the magic __clone method. * * Reset SQL query and tables collected. * Reset fields and GROUP BY. * Ensure condition points to the new query. * Resets cached SQL query state and re-parents conditions on clone. */ public function __clone() { parent::__clone(); Loading @@ -340,6 +336,9 @@ public function __clone() { $this->sqlFields = []; $this->sqlGroupBy = []; $this->condition->setQuery($this); if (isset($this->conditionAggregate)) { $this->conditionAggregate->setQuery($this); } } /** Loading
core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php +30 −0 Original line number Diff line number Diff line Loading @@ -662,6 +662,36 @@ public function testAlterHook(): void { ]); } /** * Tests that cloning an aggregate query isolates its aggregate conditions. */ public function testCloneAggregateIsolation(): void { // Original query: groups with id count > 1 are user 2 (3 rows) and user 3 // (2 rows). $original = $this->entityStorage->getAggregateQuery() ->accessCheck(FALSE) ->aggregate('id', 'count') ->groupBy('user_id') ->conditionAggregate('id', 'count', 1, '>'); $clone = clone $original; // Tightening the clone must not affect the original's conditions. $clone->conditionAggregate('id', 'count', 2, '>'); $this->queryResult = $original->execute(); $this->assertCount(2, $this->queryResult); $this->assertResults([ ['user_id' => 2, 'id_count' => 3], ['user_id' => 3, 'id_count' => 2], ]); $this->queryResult = $clone->execute(); $this->assertCount(1, $this->queryResult); $this->assertResults([ ['user_id' => 2, 'id_count' => 3], ]); } /** * Asserts the results as expected regardless of order between and in rows. * Loading