| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace yii\debug\models\search;
- use yii\base\Model;
- use yii\data\ActiveDataProvider;
- use yii\db\ActiveRecord;
- /**
- * Search model for implementation of IdentityInterface
- *
- * @author Semen Dubina <yii2debug@sam002.net>
- * @since 2.0.10
- */
- class User extends Model
- {
- /**
- * @var Model implementation of IdentityInterface
- */
- public $identityImplement = null;
- /**
- * @inheritdoc
- */
- public function init()
- {
- if (\Yii::$app->user && \Yii::$app->user->identityClass) {
- $identityImplementation = new \Yii::$app->user->identityClass();
- if ($identityImplementation instanceof Model) {
- $this->identityImplement = $identityImplementation;
- }
- }
- parent::init();
- }
- /**
- * @inheritdoc
- */
- public function __get($name)
- {
- return $this->identityImplement->__get($name);
- }
- /**
- * @inheritdoc
- */
- public function __set($name, $value)
- {
- return $this->identityImplement->__set($name, $value);
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [[array_keys($this->identityImplement->getAttributes()), 'safe']];
- }
- /**
- * @inheritdoc
- */
- public function attributes()
- {
- return $this->identityImplement->attributes();
- }
- /**
- * @inheritdoc
- */
- public function search($params)
- {
- if ($this->identityImplement instanceof ActiveRecord) {
- return $this->serachActiveDataProvider($params);
- }
- return null;
- }
- /**
- * Search method for ActiveRecord
- * @param array $params the data array to load model.
- * @return ActiveDataProvider
- */
- private function serachActiveDataProvider($params)
- {
- /** @var ActiveRecord $model */
- $model = $this->identityImplement;
- $query = $model::find();
- $dataProvider = new ActiveDataProvider([
- 'query' => $query,
- ]);
- if (!($this->load($params) && $this->validate())) {
- return $dataProvider;
- }
- foreach ($model::getTableSchema()->columns as $attribute => $column) {
- if ($column->phpType === 'string') {
- $query->andFilterWhere(['like', $attribute, $model->getAttribute($attribute)]);
- } else {
- $query->andFilterWhere([$attribute => $model->getAttribute($attribute)]);
- }
- }
- return $dataProvider;
- }
- }
|