public function actionDelQuestion($qid) { $question = Question::model()->with('tags')->findByPk($qid); foreach ($question->tags as $key => $tag) { $tag->frequency--; $tag->save(); } Question::model()->deleteByPk($qid); QueTag::model()->deleteAll("questionId=:qid", array(':qid' => $qid)); $answers = Answer::model()->findAll("questionId=:qid", array(':qid' => $qid)); foreach ($answers as $key => $ans) { LikeAns::model()->deleteAll("answerId=:aid", array(':aid' => $ans->id)); $ans->delete(); } LikeQue::model()->deleteAll("questionId=:qid", array(':qid' => $qid)); $userInfo = User::model()->findByPk(Yii::app()->user->id); $userInfo->score -= 5; $userInfo->lv = $userInfo->getLevel(); if ($userInfo->save()) { Yii::app()->user->setFlash('success', '问题删除成功~积分-5~'); } $this->redirect(Yii::app()->user->getReturnUrl()); }
public function edit() { $userInfo = User::model()->findByPk(Yii::app()->user->id); $userInfo->credit += $this->credit; $this->attributes = $_POST['Question']; $this->updateTime = date("Y-m-d H:i:s"); if (!$this->validate() || !$this->save()) { return false; } $userInfo->credit -= $this->credit; if (!$userInfo->save()) { return false; } $_old = QueTag::model()->findAll('questionId=:qid', array(':qid' => $this->id)); $_oldTags = array(); foreach ($_old as $key => $quetag) { $_oldTags[] = Tag::model()->findByPk($quetag->tagId)->name; } $_tagArray = explode(',', $_POST['Question']['tags']); $_delTags = array_diff($_oldTags, $_tagArray); $_newTags = array_diff($_tagArray, $_oldTags); $tagModel = Tag::model(); foreach ($_newTags as $key => $name) { $tag = $tagModel->find('name=:n', array(':n' => $name)); if ($tag == NULL) { $tag = new Tag(); $tag->name = $name; } else { $tag->frequency++; } if (!$tag->save()) { return false; } $queTag = new QueTag(); $queTag->questionId = $this->id; $queTag->tagId = $tag->id; if (!$queTag->save()) { return false; } } foreach ($_delTags as $key => $name) { $tag = Tag::model()->find('name=:name', array(':name' => $name)); $tag->frequency--; if (!$tag->save()) { return false; } $queTag = QueTag::model()->find('questionId=:qid and tagId=:tid', array(':qid' => $this->id, ':tid' => $tag->id)); if (!$queTag->delete()) { return false; } } Yii::app()->user->setFlash('success', '问题修改成功~'); return true; }