コード例 #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SelectedBlog::find();
     $selectedBlogTable = SelectedBlog::tableName();
     $blogTable = Post::tableName();
     $query->joinWith(['post' => function ($query) use($blogTable) {
         $query->from(['post' => $blogTable]);
     }]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]);
     // enable sorting for the related columns
     $addSortAttributes = ["post.title"];
     foreach ($addSortAttributes as $addSortAttribute) {
         $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]];
     }
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'post_id' => $this->post_id]);
     $query->andFilterWhere(['like', 'post.title', $this->getAttribute('post.title')]);
     return $dataProvider;
 }
コード例 #2
0
 /**
  * Get block with last 3 blog posts selected by site, and 3 last blog posts, which are not in selected
  * @return array Data
  */
 public static function getBlogPosts($cache = true)
 {
     if ($cache) {
         $cacheBlock = CacheBlock::find()->where(['machine_name' => 'lastBlogPosts'])->one();
         if (isset($cacheBlock)) {
             return ['view' => '@frontend/views/blocks/cache_block', 'data' => ['content' => $cacheBlock->content]];
         }
     }
     $postTable = Post::tableName();
     $selectedBlogsTable = SelectedBlog::tableName();
     $selectedBlogs = Post::find()->innerJoin($selectedBlogsTable, "{$postTable}.id = {$selectedBlogsTable}.post_id")->where(['is_public' => 1])->orderBy(['created_at' => SORT_DESC])->limit(3)->all();
     $selectedID = [];
     foreach ($selectedBlogs as $blog) {
         $selectedID[] = $blog->id;
     }
     $posts = Post::find()->where(['is_public' => 1, 'content_category_id' => Post::CATEGORY_BLOG])->andWhere(['not in', 'id', $selectedID])->orderBy(['created_at' => SORT_DESC])->limit(3)->all();
     $block = ['view' => '@frontend/views/blocks/blog_block', 'data' => compact('posts', 'selectedBlogs')];
     if (!$cache) {
         $view = new \yii\base\View();
         return $view->renderFile($block['view'] . '.php', $block['data']);
     }
     return $block;
 }
コード例 #3
0
 /**
  * Finds the SelectedBlog model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return SelectedBlog the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = SelectedBlog::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
コード例 #4
0
ファイル: Post.php プロジェクト: alexsynytskiy/Dynamomania
 /**
  * Check type model of post if it blog
  * 
  * @return boolean 
  */
 public function isSelected()
 {
     $selectedBlogs = SelectedBlog::find()->where(['post_id' => $this->id])->one();
     return isset($selectedBlogs);
 }