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; }