/** * Выставляет лайки комментариям * @return bool */ public function actionLikes() { $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT); $model = Comment::findOne(['id' => $id]); if (null === $model) { return false; } $model->setLike($_POST['like']); if (!$model->canLiked()) { return; } if ($model->save()) { Yii::$app->response->format = Response::FORMAT_JSON; return ['likes' => $model->getLikes()]; } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return Comment the loaded model * @throws \CHttpException */ public function findModel($id) { $model = Comment::findOne($id); if ($model === null) { throw new NotFoundHttpException(404, 'The requested page does not exist.'); } return $model; }