Issue #3015691 by hchonov, mondrake: entityQueryAggregate queries cannot be executed more than once

8.7.x
Nathaniel Catchpole 2018-11-28 11:56:45 +00:00
parent e58e520bba
commit 576bc7fea7
2 changed files with 42 additions and 0 deletions

View File

@ -21,6 +21,13 @@ class Query extends QueryBase implements QueryInterface {
*/
protected $sqlQuery;
/**
* The Tables object for this query.
*
* @var \Drupal\Core\Entity\Query\Sql\TablesInterface
*/
protected $tables;
/**
* An array of fields keyed by the field alias.
*
@ -101,6 +108,9 @@ class Query extends QueryBase implements QueryInterface {
$simple_query = FALSE;
}
$this->sqlQuery = $this->connection->select($base_table, 'base_table', ['conjunction' => $this->conjunction]);
// Reset the tables structure, as it might have been built for a previous
// execution of this query.
$this->tables = NULL;
$this->sqlQuery->addMetaData('entity_type', $this->entityTypeId);
$id_field = $this->entityType->getKey('id');
// Add the key field for fetchAllKeyed().

View File

@ -541,6 +541,38 @@ class EntityQueryAggregateTest extends EntityKernelTestBase {
}
/**
* Tests preparing a query and executing twice.
*/
public function testRepeatedExecution() {
$query = $this->entityStorage->getAggregateQuery()
->groupBy('user_id');
$this->queryResult = $query->execute();
$this->assertResults([
['user_id' => 1],
['user_id' => 2],
['user_id' => 3],
]);
$entity = $this->entityStorage->create([
'id' => 7,
'user_id' => 4,
'field_test_1' => 42,
'field_test_2' => 68,
]);
$entity->enforceIsNew();
$entity->save();
$this->queryResult = $query->execute();
$this->assertResults([
['user_id' => 1],
['user_id' => 2],
['user_id' => 3],
['user_id' => 4],
]);
}
/**
* Asserts the results as expected regardless of order between and in rows.
*