Exemplo n.º 1
0
 public function actionIndex()
 {
     //руссификация месяцев
     $ru_monthes = ['1' => 'Январь', '2' => 'Февраль', '3' => 'Март', '4' => 'Апрель', '5' => 'Май', '6' => 'Июнь', '7' => 'Июль', '8' => 'Август', '9' => 'Сентябрь', '10' => 'Октябрь', '11' => 'Ноябрь', '12' => 'Декабрь'];
     //месяцы и годы с учетом количества записей
     $monthes = News::find()->select(['COUNT(*) AS sum', 'month(date) as month', 'year(date) AS year'])->groupBy('month(date),year(date)')->orderBy('YEAR(date) ASC, month(date) ASC')->asArray()->all();
     //категории с учетом количества новостей
     $categories = Categories::find()->select(['count(news.id) AS count', 'categories.title', 'categories.id'])->leftJoin('news', 'news.category_id = categories.id')->groupBy("categories.id")->asArray()->all();
     //новости
     $query = News::find()->select(['news.id', 'news.title as n_title', 'news.text', 'news.date', 'categories.title as c_title'])->leftJoin('categories', 'categories.id = news.category_id')->orderBy('news.date DESC');
     $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 5]);
     $news = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
     //получение уникальных значений годов
     $years = array_unique(ArrayHelper::getColumn($monthes, 'year'));
     $calendar = array();
     foreach ($monthes as $month) {
         foreach ($years as $year) {
             if ($month['year'] == $year) {
                 $calendar[$year][] = ['month' => $month['month'], 'sum' => $month['sum']];
             }
         }
     }
     return $this->render('index', ['calendar' => $calendar, 'categories' => $categories, 'news' => $news, 'pages' => $pages, 'ru_monthes' => $ru_monthes]);
 }
Exemplo n.º 2
0
 public function getCategories()
 {
     return $this->hasOne(Categories::className(), ['id' => 'category_id']);
 }