Exemple #1
0
 /**
  * 保存或插入内容的标签
  * @param string|array $tags 标签
  * @return bool
  * @throws ErrorException
  * @throws \Exception
  */
 public function saveTags($tags)
 {
     if (is_string($tags)) {
         if (($tags = trim($tags)) === '') {
             $tags = [];
         } else {
             $tags = explode(',', $tags);
         }
     }
     //对标签去除空格并去重
     $tags = array_unique(array_map('trim', $tags));
     //如果标签为空并且是新增内容,则直接返回真
     if (empty($tags) && !$this->id) {
         return true;
     }
     $relationshipModel = new Relationship();
     $metaModel = new Meta(['scenario' => Meta::SCENARIO_TAG]);
     //取出Meta表已经有的同名标签
     $existTags = $metaModel->find()->where(['type' => Meta::TYPE_TAG])->andFilterWhere(['in', 'name', $tags])->select(['name'])->asArray()->column();
     //取出数据库没有的标签名称(要添加到Meta表的标签)
     $newTags = array_diff($tags, $existTags);
     if (!empty($newTags)) {
         //如果有新增的标签
         foreach ($newTags as $tag) {
             $model = clone $metaModel;
             $model->setAttributes(['name' => $tag]);
             if (!$model->insert()) {
                 throw new ErrorException(current($model->getFirstErrors()));
             }
         }
     }
     //如果标签不为空
     if (!empty($tags)) {
         //找出本内容的标签
         $contentTags = $metaModel->find()->where(['type' => Meta::TYPE_TAG, 'name' => $tags, 'status' => Meta::STATUS_ACTIVE])->select(['id', 'name'])->asArray()->all();
     } else {
         $contentTags = [];
     }
     //取出久的标签
     $oldTags = ArrayHelper::getColumn($this->tags, 'name');
     $updateCountersTagIds = [];
     if (!empty($contentTags)) {
         //准备插入的文章关联标签
         $relationshipInsertData = [];
         foreach ($contentTags as $tag) {
             //如果要加的标签之前就有则跳过
             if (in_array($tag['name'], $oldTags)) {
                 continue;
             }
             $relationshipInsertData[] = ['content_id' => $this->id, 'meta_id' => $tag['id']];
             $updateCountersTagIds[] = $tag['id'];
         }
         if (!empty($relationshipInsertData)) {
             if (!$relationshipModel->getDb()->createCommand()->batchInsert($relationshipModel->tableName(), ['content_id', 'meta_id'], $relationshipInsertData)->execute()) {
                 throw new \ErrorException('插入文章标签关联失败');
             }
         }
     }
     $deleteTagIds = [];
     foreach ($this->tags as $tag) {
         if (!in_array($tag->name, $tags)) {
             $deleteTagIds[] = $tag->id;
         }
     }
     //内容新增的标签的内容统计+1
     if (!empty($updateCountersTagIds)) {
         if (!$metaModel->updateAllCounters(['content_total' => 1], ['id' => $updateCountersTagIds])) {
             throw new ErrorException('更新标签内容统计失败');
         }
     }
     if (!empty($deleteTagIds)) {
         //内容之前的标签被删除则内容统计-1
         $metaModel->updateAllCounters(['content_total' => -1], ['id' => $deleteTagIds]);
         //然后删除内容里这些不要的标签
         $relationshipModel->deleteAll(['content_id' => $this->id, 'meta_id' => $deleteTagIds]);
     }
     return true;
 }