|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
-
-
- namespace yii\data;
-
- use Yii;
- use yii\db\ActiveQueryInterface;
- use yii\base\InvalidConfigException;
- use yii\base\Model;
- use yii\db\Connection;
- use yii\db\QueryInterface;
- use yii\di\Instance;
-
-
- class ActiveDataProvider extends BaseDataProvider
- {
-
-
- public $query;
-
-
- public $key;
-
-
- public $db;
-
-
-
-
- public function init()
- {
- parent::init();
- if (is_string($this->db)) {
- $this->db = Instance::ensure($this->db, Connection::className());
- }
- }
-
-
-
- protected function prepareModels()
- {
- 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.');
- }
- $query = clone $this->query;
- if (($pagination = $this->getPagination()) !== false) {
- $pagination->totalCount = $this->getTotalCount();
- $query->limit($pagination->getLimit())->offset($pagination->getOffset());
- }
- if (($sort = $this->getSort()) !== false) {
- $query->addOrderBy($sort->getOrders());
- }
-
- return $query->all($this->db);
- }
-
-
-
- protected function prepareKeys($models)
- {
- $keys = [];
- if ($this->key !== null) {
- foreach ($models as $model) {
- if (is_string($this->key)) {
- $keys[] = $model[$this->key];
- } else {
- $keys[] = call_user_func($this->key, $model);
- }
- }
-
- return $keys;
- } elseif ($this->query instanceof ActiveQueryInterface) {
-
- $class = $this->query->modelClass;
- $pks = $class::primaryKey();
- if (count($pks) === 1) {
- $pk = $pks[0];
- foreach ($models as $model) {
- $keys[] = $model[$pk];
- }
- } else {
- foreach ($models as $model) {
- $kk = [];
- foreach ($pks as $pk) {
- $kk[$pk] = $model[$pk];
- }
- $keys[] = $kk;
- }
- }
-
- return $keys;
- } else {
- return array_keys($models);
- }
- }
-
-
-
- protected function prepareTotalCount()
- {
- 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.');
- }
- $query = clone $this->query;
- return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db);
- }
-
-
-
- public function setSort($value)
- {
- parent::setSort($value);
- if (($sort = $this->getSort()) !== false && $this->query instanceof ActiveQueryInterface) {
-
- $model = new $this->query->modelClass;
- if (empty($sort->attributes)) {
- foreach ($model->attributes() as $attribute) {
- $sort->attributes[$attribute] = [
- 'asc' => [$attribute => SORT_ASC],
- 'desc' => [$attribute => SORT_DESC],
- 'label' => $model->getAttributeLabel($attribute),
- ];
- }
- } else {
- foreach ($sort->attributes as $attribute => $config) {
- if (!isset($config['label'])) {
- $sort->attributes[$attribute]['label'] = $model->getAttributeLabel($attribute);
- }
- }
- }
- }
- }
- }
|