Skip to content

Commit a1807f9

Browse files
authored
Merge pull request #1 from cepaim/cepaim-prepareModels-union
Update ActiveDataProvider::prepareModels() to avoid SQL error with UNION queries
2 parents 34d2396 + 6356e60 commit a1807f9

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

framework/data/ActiveDataProvider.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,29 @@ protected function prepareModels()
101101
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.');
102102
}
103103
$query = clone $this->query;
104+
105+
$has_union = $query->union && count($query->union);
104106
if (($pagination = $this->getPagination()) !== false) {
105107
$pagination->totalCount = $this->getTotalCount();
106108
if ($pagination->totalCount === 0) {
107109
return [];
108110
}
109-
$query->limit($pagination->getLimit())->offset($pagination->getOffset());
111+
if ($has_union) {
112+
$query->union[count($query->union)-1]['query']->limit($pagination->getLimit())->offset($pagination->getOffset());
113+
} else {
114+
$query->limit($pagination->getLimit())->offset($pagination->getOffset());
115+
}
110116
}
111117
if (($sort = $this->getSort()) !== false) {
112118
$query->addOrderBy($sort->getOrders());
119+
}
120+
if ($has_union) {
121+
if ($query->orderBy) {
122+
$query->union[count($query->union)-1]['query']->addOrderBy($query->orderBy);
123+
$query->orderBy = null;
124+
}
113125
}
114-
126+
115127
return $query->all($this->db);
116128
}
117129

0 commit comments

Comments
 (0)