protected function prepareModels()
{
$sort = $this->getSort();
$pagination = $this->getPagination();
if ($pagination === false && $sort === false) {
return $this->db->createCommand($this->sql, $this->params)->queryAll();
}
$sql = $this->sql;
$orders = [];
$limit = $offset = null;
if ($sort !== false) {
$orders = $sort->getOrders();
$pattern = '/\\s+order\\s+by\\s+([\\w\\s,\\.]+)$/i';
if (preg_match($pattern, $sql, $matches)) {
array_unshift($orders, new Expression($matches[1]));
$sql = preg_replace($pattern, '', $sql);
}
}
if ($pagination !== false) {
$pagination->totalCount = $this->getTotalCount();
$limit = $pagination->getLimit();
$offset = $pagination->getOffset();
}
$sql = $this->db->getQueryBuilder()->buildOrderByAndLimit($sql, $orders, $limit, $offset);
return $this->db->createCommand($sql, $this->params)->queryAll();
}