/** * Finds the News model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return News the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = News::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionCommentsWidget($id, $showButtons = true) { $news = News::findOne($id); if (!$news) { throw new NotFoundHttpException("Новость с идентификатром {$id} не найдена!"); } return $this->renderPartial('comments-widget', ['news' => $news, 'showButtons' => $showButtons, 'comments' => new ActiveDataProvider(['query' => $news->getComments(), 'pagination' => ['pageSize' => 0], 'sort' => ['defaultOrder' => ['date' => SORT_ASC]]])]); }
public function save() { $news = new News(); if (!empty($this->id)) { $news = News::findOne($this->id); } $news->setAttributes(['title' => $this->title, 'meta_keywords' => $this->metaKeywords, 'meta_description' => $this->metaDescription, 'categoryID' => $this->category, 'favorite' => $this->favorite, 'published' => $this->published, 'publishDate' => strtotime($this->publishDate), 'deleted' => $this->deleted, 'textPreview' => preg_replace('/"=""/', '', $this->textPreview), 'text' => preg_replace('/"=""/', '', $this->textExtension), 'author' => $this->author, 'link' => $this->link, 'moderatedComments' => $this->moderatedComments, 'language' => $this->language, 'genre' => $this->genre], false); if (!$news->save(false)) { return false; } $this->id = $news->id; return true; }
public function actionChangeattribute() { if (!\Yii::$app->request->isAjax) { throw new MethodNotAllowedHttpException(); } \Yii::$app->response->format = 'raw'; $news = News::findOne(\Yii::$app->request->post("newsID")); if (!$news) { throw new NotFoundHttpException(); } $attribute = \Yii::$app->request->post("attribute"); if (!in_array($attribute, ['favorite', 'deleted', 'published', 'checked', 'moderatedComments'])) { throw new ForbiddenHttpException(); } $news->{$attribute} = $news->{$attribute} == 1 ? 0 : 1; if ($news->save(false)) { return $news->{$attribute}; } return 0; }