Skip to content

Commit

Permalink
Fixes #40: Added public method `yii\data\ActiveDataProvider::prepareQ…
Browse files Browse the repository at this point in the history
…uery()` for debug facilitation
  • Loading branch information
np25071984 authored and samdark committed Jan 27, 2019
1 parent e93f97c commit bed8118
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Yii Active Record Change Log
3.0.0 under development
-----------------------

- Enh #40: Added public method `yii\data\ActiveDataProvider::prepareQuery()` for debug facilitation (GHopperMSK)
- Enh #34: Adjustments to new DI and events replacements (fabriziocaldarelli)
21 changes: 16 additions & 5 deletions src/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
* The following is an example of using ActiveDataProvider to provide ActiveRecord instances:
*
* ```php
* $provider = new ActiveDataProvider(
* $provider = new ActiveDataProvider(
* Yii::$app->db,
* Post::find()
* );
* $provider->pagination' => [
* 'pageSize' => 20,
* ];
*
*
*
* // get the posts in the current page
* $posts = $provider->getModels(); // or $provider->models
Expand All @@ -47,7 +47,7 @@
* $provider->pagination' => [
* 'pageSize' => 20,
* ];
*
*
*
* // get the posts in the current page
* $posts = $provider->getModels(); // or $provider->models
Expand Down Expand Up @@ -100,6 +100,17 @@ public function __construct(ConnectionInterface $db, QueryInterface $query)
* {@inheritdoc}
*/
protected function prepareModels()
{
$query = $this->prepareQuery();
return $query->all($this->db);
}

/**
* Prepares the sql-query that will get the data for current page.
* @return QueryInterface
* @throws InvalidConfigException
*/
public function prepareQuery()
{
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.');
Expand All @@ -108,15 +119,15 @@ protected function prepareModels()
if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount();
if ($pagination->totalCount === 0) {
return [];
$query->emulateExecution();
}
$query->limit($pagination->getLimit())->offset($pagination->getOffset());
}
if (($sort = $this->getSort()) !== false) {
$query->addOrderBy($sort->getOrders());
}

return $query->all($this->db);
return $query;
}

/**
Expand Down

0 comments on commit bed8118

Please sign in to comment.