public function afterDelete() { parent::afterDelete(); //修改直接子类的父级id self::updateAll(['parent' => $this->parent], 'parent=:parent AND type=:type', [':parent' => $this->mid, ':type' => static::TYPE]); //删除和文章的关联 Relationship::deleteAll(['mid' => $this->mid]); }
public function actionDeleteUser() { $selection = (array) Yii::$app->request->post('selection'); foreach ($selection as $id) { if ($id != Yii::$app->params['adminId']) { User::deleteAll(['id' => $id]); Post::deleteAll(['user_id' => $id]); PostNotification::deleteAll(['action_id' => $id]); PostNotification::deleteAll(['receiver_id' => $id]); Comment::deleteAll(['user_id' => $id]); Like::deleteAll(['user_id' => $id]); Message::deleteAll(['sender_id' => $id]); Message::deleteAll(['receiver_id' => $id]); Relationship::deleteAll(['user_id_1' => $id]); Relationship::deleteAll(['user_id_2' => $id]); RelationshipNotification::deleteAll(['action_id' => $id]); RelationshipNotification::deleteAll(['receive_id' => $id]); Schedule::deleteAll(['own_id' => $id]); ScheduleNotification::deleteAll(['action_id' => $id]); ScheduleNotification::deleteAll(['receiver_id' => $id]); } } return $this->render('user-manage'); }
public function deleteTags($isCount = true) { //获取文章标签 $existTags = $this->tags; //删除标签 foreach ($existTags as $v) { Relationship::deleteAll(['cid' => $this->cid, 'mid' => $v->mid]); //更新标签的文章数 if ($v->count > 0 && $isCount) { Tag::updateAllCounters(['count' => '-1'], ['mid' => $v->mid]); } } }
/** * 保存或插入内容的标签 * @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; }
public function afterDelete() { parent::afterDelete(); //删除和文章的关联 Relationship::deleteAll(['mid' => $this->mid]); }