From 1d72bee71cacf01135ca3bf280a3d0ba3a24ef16 Mon Sep 17 00:00:00 2001 From: xchenhao Date: Fri, 4 Oct 2019 14:01:05 +0800 Subject: [PATCH] get newest articles --- common/models/Article.php | 12 +++++++----- frontend/controllers/ArticleController.php | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/common/models/Article.php b/common/models/Article.php index 04e7d17..d8e7310 100644 --- a/common/models/Article.php +++ b/common/models/Article.php @@ -45,8 +45,9 @@ class Article extends \yii\db\ActiveRecord // 版头图 const ATTR_BANNER = 0b10; - // 首页播放量最高的文章数量 - const HOMEPAGE_COUNT_TOP_VIEW_ARTICLE = 9; + // 首页文章数量 + const HOMEPAGE_ARTICLE_COUNT = 9; + // 版头图数量 const BANNER_COUNT = 3; @@ -139,14 +140,15 @@ class Article extends \yii\db\ActiveRecord } /** - * 获取播放量最高的文章 + * 获取文章 * * @param int $page * @param int $page_size + * @param string $order_by 例:id, create_time, view_count * @return array * @throws \Exception */ - public static function getTopViewArticles(int $page = 1, int $page_size = self::HOMEPAGE_COUNT_TOP_VIEW_ARTICLE): array + public static function getArticles(int $page = 1, int $page_size = self::HOMEPAGE_ARTICLE_COUNT, string $order_by = 'id'): array { if ($page_size < 0) { throw new \Exception('参数错误'); @@ -156,7 +158,7 @@ class Article extends \yii\db\ActiveRecord ->select('id, category_id, title, intro, cover, create_time') ->where(['status' => self::STATUS_PASS]) ->andWhere('attr & :banner = 0', [':banner' => self::ATTR_BANNER]) - ->orderBy('view_count DESC'); + ->orderBy([$order_by => SORT_DESC]); $items = $query ->offset($page_size * ($page - 1)) ->limit($page_size) diff --git a/frontend/controllers/ArticleController.php b/frontend/controllers/ArticleController.php index 608a7c3..9973769 100755 --- a/frontend/controllers/ArticleController.php +++ b/frontend/controllers/ArticleController.php @@ -54,9 +54,9 @@ class ArticleController extends Controller * @return string * @throws \Exception */ - public function actionIndex(int $page = 1, $page_size = Article::HOMEPAGE_COUNT_TOP_VIEW_ARTICLE, $list = 0) + public function actionIndex(int $page = 1, $page_size = Article::HOMEPAGE_ARTICLE_COUNT, $list = 0) { - $articles = Article::getTopViewArticles($page, $page_size); + $articles = Article::getArticles($page, $page_size); $articles['pagination']->params = array_merge($_GET, ['list' => $list]); $banners = Article::getBanners(); return $this->render('index', [ @@ -90,7 +90,7 @@ class ArticleController extends Controller * @param integer $list 文章显示方式(0 卡片,1 列表) * @return string */ - public function actionList(int $category_id, int $page = 1, $page_size = Article::HOMEPAGE_COUNT_TOP_VIEW_ARTICLE, $list = 0) + public function actionList(int $category_id, int $page = 1, $page_size = Article::HOMEPAGE_ARTICLE_COUNT, $list = 0) { $category = Category::findOne(['id' => $category_id]); $view_tpl = 'list'; -- GitLab