Example #1
0
 public function init()
 {
     parent::init();
     $this->language or $this->language = Yii::$app->language;
     $this->_tags = (new Query())->select('t.id, t.title, t.alias, t.language, count(t2m.item_id) AS weight')->from(Tag::tableName() . ' t')->innerJoin(Tag::pivotTableName() . ' t2m', 't.id=t2m.tag_id')->where(['t.language' => $this->language])->groupBy('t.id')->all();
     shuffle($this->_tags);
 }
 /**
  * Tags relation
  * @return static
  */
 public function getTags()
 {
     return is_array($this->_tags) ? Tag::findAll(['id' => $this->_tags]) : $this->owner->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable(Tag::pivotTableName(), ['item_id' => 'id'], function ($query) {
         /** @var $query ActiveQuery */
         $query->andWhere(['item_class' => $query->modelClass]);
     })->indexBy('id');
 }