Example #1
0
 /**
  * Finds the Post model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Post the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($slug)
 {
     if (($model = Post::findOne(['slug' => $slug])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #2
0
 public function actionGenerateSitemap()
 {
     $sitemap = new Sitemap(Yii::getAlias('@app/web') . '/sitemap.xml');
     $posts = Post::findAll(['status' => 1]);
     foreach ($posts as $post) {
         $sitemap->addItem(Url::to(['/blog/post/view', 'slug' => $post->slug], true), time(), Sitemap::DAILY);
     }
     $sitemap->write();
 }
Example #3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Post::find()->orderBy('id DESC');
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'author_id' => $this->author_id, 'created' => $this->created, 'updated' => $this->updated, 'views' => $this->views, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'anotation', $this->anotation])->andFilterWhere(['like', 'text', $this->text]);
     return $dataProvider;
 }
Example #4
0
 public function init()
 {
     parent::init();
     $posts = Post::find()->where(['status' => 1])->orderBy(['views' => SORT_DESC])->all();
     echo $this->render('posts', ['posts' => $posts]);
 }
Example #5
0
 public function getPosts()
 {
     return $this->hasMany(Post::className(), ['id' => 'post_id'])->viaTable('posts_categories', ['category_id' => 'id'])->where(['post.status' => 1]);
 }