Skip to content

Commit

Permalink
Replace resetting with real wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Izumi-kun committed Jan 16, 2025
1 parent 96ff732 commit dcf8674
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
35 changes: 20 additions & 15 deletions framework/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,27 @@ protected function createQueryWrapper(): QueryInterface
if (!$this->query instanceof QueryInterface) {
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
}
$wrapper = clone $this->query;
if ($wrapper instanceof Query && !empty($wrapper->union)) {
$wrapper->where = [];
$wrapper->limit = null;
$wrapper->offset = null;
$wrapper->orderBy = [];
$wrapper->selectOption = null;
$wrapper->distinct = false;
$wrapper->groupBy = [];
$wrapper->join = [];
$wrapper->having = [];
$wrapper->union = [];
$wrapper->params = [];
$wrapper->withQueries = [];
$wrapper->select('*')->from(['q' => $this->query]);
if (!$this->query instanceof Query || empty($this->query->union)) {
return clone $this->query;
}

$wrapper = new class extends Query {
/**
* @var Query
*/
public $wrappedQuery;
/**
* @inheritDoc
*/
public function all($db = null)
{
return $this->wrappedQuery->populate(parent::all($db));
}
};
$wrapper->select('*')->from(['q' => $this->query]);
$wrapper->wrappedQuery = $this->query;
$wrapper->emulateExecution = $this->query->emulateExecution;

return $wrapper;
}

Expand Down
1 change: 1 addition & 0 deletions tests/framework/data/ActiveDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public function testPaginationWithUnionQuery()
$q1 = Item::find()->where(['category_id' => 2])->with('category');
$q2 = Item::find()->where(['id' => [2, 4]]);
$provider = new ActiveDataProvider([
'db' => $this->getConnection(),
'query' => $q1->union($q2)->indexBy('id'),
]);
$pagination = $provider->getPagination();
Expand Down

0 comments on commit dcf8674

Please sign in to comment.