コード例 #1
0
ファイル: LikeBehavior.php プロジェクト: buildshop/bs-common
 public function checkUserLiked()
 {
     $user = Yii::app()->user;
     if (!$user->isGuest) {
         $model = LikeModel::model()->findByAttributes(array('user_id' => $user->id, 'model' => $this->getClassName(), 'object_id' => $this->getOwner()->primaryKey));
         if (!isset($model)) {
             return true;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
コード例 #2
0
 function actionLike()
 {
     $user_id = $_REQUEST['uid'];
     $style_id = $_REQUEST['sid'];
     //记录存储到like表
     $likeModel = new LikeModel();
     $ret = LikeModel::model()->find('user_id=:userid and style_id=:styleid', array(':userid' => $user_id, ':styleid' => $style_id));
     if (!isset($ret)) {
         $likeModel->user_id = $user_id;
         $likeModel->style_id = $style_id;
         $likeModel->like_time = time();
         if (!$likeModel->save()) {
             echo "like record saved fail!";
         }
     } else {
         echo json_encode(array('result' => 0, 'comment' => "you have liked it!"));
         return;
     }
     //修改style表,like_num 加1
     $styleModel = new StyleModel();
     $connection = Yii::app()->db;
     $sql = "select like_num from tbl_style where _id = :style_id";
     $command = $connection->createCommand($sql);
     $tmp = $command->query(array(':style_id' => $style_id))->readAll();
     $styleModel->updateByPk($style_id, array('like_num' => $tmp[0]["like_num"] + 1));
     echo json_encode(array('result' => 1, 'res' => array('style_id' => $likeModel->style_id, 'like_num' => $tmp[0]["like_num"] + 1)));
 }