public function validateAttribute($model, $attribute) { $value = $model->{$attribute}; if (isset($model->id) || isset($model->item_id)) { /** @noinspection PhpUndefinedFieldInspection */ $modelId = isset($model->id) ? $model->id : $model->item_id; if (Device::find()->where(['!=', 'id', $modelId])->andWhere(['text_id' => $value])->exists()) { $model->addError($attribute, $this->message); } } else { if (Device::find()->where(['text_id' => $value])->exists()) { $model->addError($attribute, $this->message); } } }
/** * Creates data provider instance with search query applied * @param $params * @return ActiveDataProvider */ public function search($params) { $query = parent::find(); if (!isset($params['sort'])) { $query->orderBy(['title' => SORT_ASC]); } $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['like', 'title', $this->title]); $query->andFilterWhere(['like', 'text_id', $this->text_id]); return $dataProvider; }
public function init() { parent::init(); $this->_items = Device::find()->where(['like', 'text_id', $this->keyword])->all(); }