public function testGetPostsWithCategory() { $category = $this->categoryModel->findOne(1); $expectedPosts = $category->getPosts(); $actualPosts = $this->postModel->findAll(['category_id' => 1, 'publish_status' => Post::STATUS_PUBLISH]); $this->assertEquals($expectedPosts->count, count($actualPosts)); }
public function testGetPublishedPosts() { $posts = $this->postModel->getPublishedPosts(); $this->assertInstanceOf('yii\\data\\ActiveDataProvider', $posts); $count = $this->postModel->findAll(['publish_status' => Post::STATUS_PUBLISH]); $this->assertEquals($posts->count, count($count)); }
/** * Delete multiple existing Post model. * For ajax request will return json object * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionBulkDelete() { $request = Yii::$app->request; $pks = $request->post('pks'); // Array or selected records primary keys foreach (Post::findAll(json_decode($pks)) as $model) { $model->delete(); } }
/** * Get block with 3 blog posts, which are best by rating during last 3 months * @return array Data */ public static function getBlogPostsByRating($cache = true) { if ($cache) { $cacheBlock = CacheBlock::find()->where(['machine_name' => 'blogPostsByRating'])->one(); if (isset($cacheBlock)) { return ['view' => '@frontend/views/blocks/cache_block', 'data' => ['content' => $cacheBlock->content]]; } } $connection = Yii::$app->db; $blogsIDQuery = 'SELECT id FROM posts WHERE created_at > DATE_SUB(NOW(), INTERVAL 90 day) AND content_category_id = ' . Post::CATEGORY_BLOG; //INNER JOIN votes ON post.id = votes.voteable_id AND votes.voteable_type = '.Vote::VOTEABLE_POST.' $cmd = $connection->createCommand($blogsIDQuery); $bestBlogs = $cmd->queryAll(); $ratingArray = []; foreach ($bestBlogs as &$blog) { $blog['rating'] = Vote::getRating($blog['id'], Vote::VOTEABLE_POST); } for ($i = 0; $i < count($bestBlogs) - 1; $i++) { for ($j = $i + 1; $j < count($bestBlogs); $j++) { if ($bestBlogs[$j]['rating'] > $bestBlogs[$i]['rating']) { $temp = $bestBlogs[$j]; $bestBlogs[$j] = $bestBlogs[$i]; $bestBlogs[$i] = $temp; } } } $count = 3; if (count($bestBlogs) < 3) { $count = count($bestBlogs); } $best3Blogs = []; for ($i = 0; $i < $count; $i++) { $best3Blogs[] = $bestBlogs[$i]['id']; } $blogs = Post::findAll($best3Blogs); $block = ['view' => '@frontend/views/blocks/blog_block_rating', 'data' => compact('blogs')]; if (!$cache) { $view = new \yii\base\View(); return $view->renderFile($block['view'] . '.php', $block['data']); } return $block; }