public function actionNews($slug = null) { if ($slug === null) { $model = new News('search'); $model->unsetAttributes(); $model->isList = true; $model->category_id = Yii::app()->request->getParam('category_id'); $this->render('news', array('model' => $model)); } else { $model = $this->loadNews($slug); $model->saveCounters(array('countViews' => 1)); // to memcache mb $comment = new NewsComment(); if (isset($_POST['ajax']) && $_POST['ajax'] === 'newsComment-form') { echo CActiveForm::validate($comment); Yii::app()->end(); } if (isset($_POST['NewsComment'])) { $comment->attributes = $_POST['NewsComment']; $comment->news_id = $model->id; $comment->user_id = Yii::app()->user->id; if ($comment->validate() && $comment->preSave()) { $comment->unsetAttributes(); Yii::app()->user->setFlash('comment_success', true); $this->redirect(array('/site/news', 'slug' => $model->slug, '#' => 'comments')); } } $criteria = new CDbCriteria(); $criteria->compare('news_id', $model->id); $criteria->order = '`id` ASC'; $comments = NewsComment::model()->findAll($criteria); $this->render('news_inner', array('model' => $model, 'comment' => $comment, 'comments' => $comments)); } }
public function loadModel($id) { $model = NewsComment::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'Запись не найдена.'); } return $model; }