/** * 将指定文章的所有标签文章数加1 * @param $article_id * @return bool */ public static function updateTagArticleCounts($article_id) { $articleTag = ArticleTag::find()->where(['article_id' => $article_id])->all(); if (!$articleTag) { return false; } foreach ($articleTag as $v) { Tag::updateAllCounters(['article_count' => 1], ['id' => $v->tag_id]); } return true; }
public function insertTags($tags, $beforeCount = true, $afterCount = true) { if (!is_array($tags)) { return false; } $this->deleteTags($beforeCount); //先删除标签 //插入新标签 $tagIds = Tag::scanTags($tags); if ($tagIds) { foreach ($tagIds as $v) { $model = new Relationship(); $model->cid = $this->cid; $model->mid = $v; $model->insert(false); if ($afterCount) { //更新标签文章数 Tag::updateAllCounters(['count' => 1], ['mid' => $v]); } } } return true; }