public function translated() { $messageTableName = Message::tableName(); $query = Message::find()->select($messageTableName . '.id'); $i = 0; foreach (Yii::$app->getI18n()->languages as $language) { if ($i === 0) { $query->andWhere($messageTableName . '.language = :language and ' . $messageTableName . '.translation is not null', [':language' => $language]); } else { $query->innerJoin($messageTableName . ' t' . $i, 't' . $i . '.id = ' . $messageTableName . '.id and t' . $i . '.language = :language and t' . $i . '.translation is not null', [':language' => $language]); } $i++; } $ids = $query->indexBy('id')->all(); $this->andWhere(['in', 'id', array_keys($ids)]); return $this; }
/** * @return \common\widgets\yii2TranslatePanel\models\query\SourceMessageQuery */ public function translated() { $table = Message::tableName(); $query = Message::find()->select("{$table}.id"); $i = 0; foreach (Yii::$app->getI18n()->languages as $language) { if ($i === 0) { $query->andWhere("{$table}.language = :language and {$table}.translation is not null", [':language' => $language]); } else { $query->innerJoin("{$table} t{$i}", "t{$i}.id = {$table}.id and t{$i}.language = :language and t{$i}.translation is not null", [':language' => $language]); } $i++; } $ids = $query->indexBy('id')->all(); $this->andWhere(['in', "id", array_keys($ids)])->andWhere(['not like', 'message', '@@']); return $this; }
public function getMessages() { return $this->hasMany(Message::className(), ['id' => 'id'])->indexBy('language'); }
/** * @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; }
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; }