/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tagging::find(); // set up query with relation to `tag.name` $tagTable = Tag::tableName(); $query->joinWith(['tag' => function ($query) use($tagTable) { $query->from(['tag' => $tagTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query]); // enable sorting for the related columns $addSortAttributes = ["tag.name"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } $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(['id' => $this->id, 'tag_id' => $this->tag_id, 'taggable_id' => $this->taggable_id]); $query->andFilterWhere(['like', 'taggable_type', $this->taggable_type]); return $dataProvider; }
/** * Deletes an existing Tag model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $tag = $this->findModel($id); $taggings = Tagging::find()->where(['taggable_type' => Tagging::TAGGABLE_POST, 'tag_id' => $id])->all(); foreach ($taggings as $tagging) { $post = Post::findOne($tagging->taggable_id); if ($post) { $cached_tag_list = []; $newTags = $post->getTags(); foreach ($newTags as $newTag) { if (strcmp($tag->name, $newTag->name) !== 0) { $cached_tag_list[] = $newTag->name; } } $post->cached_tag_list = implode(', ', $cached_tag_list); $post->save(true, ['cached_tag_list']); } } Tagging::deleteAll(['tag_id' => $tag->id]); $tag->delete(); return $this->redirect(['index']); }
/** * @param int $id Tag id * * @return boolean */ public function removeTag($id) { $tagging = Tagging::find()->where(['taggable_id' => $this->id, 'tag_id' => $id, 'taggable_type' => Tagging::TAGGABLE_VIDEO])->one(); if ($tagging) { return $tagging->delete(); } return false; }