Example #1
0
 public function get_category()
 {
     $data = TblCategory::find()->all();
     $category = [];
     foreach ($data as $key => $value) {
         $tmp = $value->attributes;
         $category[$tmp['id']] = $tmp['name'];
     }
     return $category;
 }
Example #2
0
 public function actionCategory()
 {
     $category_id = Yii::$app->request->get('category');
     if (is_null($category_id)) {
         return $this->actionIndex();
     }
     $query = TblBlog::find()->where(['category_id' => $category_id]);
     $page = clone $query;
     $pages = new Pagination(['totalCount' => $query->count()]);
     $pages->setPageSize(5);
     $articles = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
     foreach ($articles as $key => $value) {
         $articles[$key]['text'] = mb_substr($value['text'], 0, 100, 'UTF-8');
     }
     $category = TblCategory::find()->where(['id' => $category_id])->asArray()->one();
     return $this->render('category', ['articles' => $articles, 'category' => $category, 'pages' => $pages]);
 }
Example #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCategory()
 {
     return $this->hasOne(TblCategory::className(), ['id' => 'category_id']);
 }
 /**
  * Finds the TblCategory model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return TblCategory the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = TblCategory::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }