public static function setCommentLike($id, $val) { $val = $val >= 0 ? 1 : -1; $rate = self::model()->findByAttributes(array('comment_id' => $id, 'user_id' => Yii::app()->user->id)); $comment = BooksComments::model()->findByPk($id); if (empty($rate)) { $rate = new CommentLike(); $rate->comment_id = $id; $rate->user_id = Yii::app()->user->id; } else { if ($rate->val == $val) { $rate->delete(); if (!empty($comment)) { $comment->updateRate(); } return 0; } } $rate->val = $val; $rate->save(); if (!empty($comment)) { $comment->updateRate(); } return $val; }