/** * Finds the TrainingCategory model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return TrainingCategory the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = TrainingCategory::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionCategory($categoryId) { $category = TrainingCategory::findOne($categoryId); if (empty($category)) { throw new NotFoundHttpException('The requested page does not exist.'); } $params = Yii::$app->request->queryParams; $params['categoryId'] = $categoryId; $searchModel = new TrainingRepository(); $dataProvider = $searchModel->search($params); return $this->render(['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'categoryName' => $category->name]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $countQuery = Training::find()->select('COUNT(*)')->where('`category_id` = `c`.`id`')->createCommand()->sql; $query = TrainingCategory::find()->from(['c' => self::tableName()])->select(['c.*', 'trainingsCount' => "({$countQuery})"]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['attributes' => ['name', 'trainingsCount'], 'defaultOrder' => ['name' => SORT_ASC]]]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere([]); $query->andFilterWhere(['like', 'c.name', $this->name]); return $dataProvider; }
public function getCategoryIdItems() { return TrainingCategory::getItems(); }