Example #1
0
 public function actionAnswer($id)
 {
     $answer = $this->findAnswer($id);
     $user = Yii::$app->user->getIdentity();
     list($result, $like) = Hate::Answer($user, $answer);
     if ($result) {
         return $this->message('提交成功!', 'success');
     } else {
         return $this->message($like ? $like->getErrors() : '提交失败!');
     }
 }
Example #2
0
 /**
  * 喜欢数据切换
  * @param User $user
  * @param ActiveRecord $model
  * @return array
  */
 protected static function toggleType(User $user, Post $model)
 {
     $data = ['target_id' => $model->id, 'target_type' => $model::TYPE, 'author_id' => $user->id, 'status' => static::STATUS_ACTIVE];
     if (!static::deleteAll($data + ['type' => static::TYPE])) {
         // 删除数据有行数则代表有数据,无行数则添加数据
         $like = new static();
         $like->setAttributes($data);
         $result = $like->save();
         if ($result) {
             // 如果是新增数据, 删除掉Hate的同类型数据
             $attributes = ['like_count' => 1];
             if (Hate::deleteAll($data + ['type' => Hate::TYPE])) {
                 // 如果有删除hate数据, hate_count也要-1
                 $attributes['hate_count'] = -1;
             }
             $model->updateCounters($attributes);
         }
         return [$result, $like];
     }
     $model->updateCounters(['like_count' => -1]);
     return [true, null];
 }
Example #3
0
 /**
  * 获取踩记录
  * @return mixed
  */
 public function getHate()
 {
     return $this->hasOne(Hate::className(), ['target_id' => 'id'])->andWhere(['uid' => Yii::$app->user->getId(), 'target_type' => static::TYPE, 'type' => 'hate']);
 }
Example #4
0
 /**
  * 获取指定用户的踩记录
  * @param null|int $authorId
  * @return mixed
  */
 public function getHate($authorId = null)
 {
     return $this->hasOne(Hate::className(), ['target_id' => 'id'])->andWhere(['target_type' => self::TYPE, 'author_id' => $authorId ?: Yii::$app->getUser()->getId()]);
 }