/** * @param array|null $params * @return ActiveDataProvider */ public function search($params) { $query = SourceMessage::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); // check and populate params if (!($this->load($params) && $this->validate())) { $query->joinWith(['messages']); return $dataProvider; } if ($this->status == static::STATUS_TRANSLATED) { $query->translated(); } if ($this->status == static::STATUS_NOT_TRANSLATED) { $query->notTranslated(); } if ($this->status == static::STATUS_DELETED) { $query->deleted(); } // search with related table // @see http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/ if (!empty($this->translation)) { $query->joinWith(['messages' => function ($q) { $q->where(['like', Message::tableName() . '.translation', $this->translation]); }]); } $query->andFilterWhere(['like', 'category', $this->category])->andFilterWhere(['like', 'message', $this->message]); return $dataProvider; }
/** * @return array|SourceMessage[] */ public static function getCategories() { return SourceMessage::find()->select('category')->distinct('category')->asArray()->all(); }
public function actionFlush() { $tableNames = [Message::tableName(), SourceMessage::tableName()]; $db = Yii::$app->getDb(); foreach ($tableNames as $tableName) { $db->createCommand()->delete($tableName)->execute(); } echo PHP_EOL . 'Done.' . PHP_EOL; }