public function run()
 {
     $models = Categories::getDb()->cache(function ($db) {
         return Categories::find()->alias('Main')->andWhere(['Main.parent_id' => $this->_parentId])->active('Main')->orderBy('Main.group, Main.title')->all();
     });
     return $this->render($this->view, ['models' => $models]);
 }
 /**
  * Lists all Products models.
  * @return mixed
  */
 public function actionIndex($id = 64, $size = null)
 {
     if (!is_null($size)) {
         Yii::$app->session->set('pageSize', (int) $size);
     }
     $ids = [];
     if (!is_null($id)) {
         $ids = \Yii::$app->cache->get('child_categories_' . md5($id));
         if (!$ids) {
             $models = Categories::find()->active()->all();
             $models = Categories::tree($models, 0, [], (int) $id);
             $models = ArrayHelper::map($models, 'id', 'title');
             $ids = array_keys($models);
             $ids[] = (int) $id;
             \Yii::$app->cache->set('child_categories_' . md5($id), $ids);
         }
     }
     $size = in_array(Yii::$app->session->get('pageSize'), [5, 60, 120, 180]) ? Yii::$app->session->get('pageSize') : 60;
     $category = !is_null($id) ? $this->findCatModel($id) : null;
     \Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => !is_null($category) ? $category->meta_description : \Yii::$app->params['description']]);
     \Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => !is_null($category) ? $category->meta_keywords : \Yii::$app->params['keywords']]);
     $dataProvider = new ActiveDataProvider(['query' => Products::getDb()->cache(function ($db) use($ids) {
         return Products::find()->joinWith(['categories', 'images'])->where(empty($ids) ? [] : ['categories.id' => $ids])->active()->orderBy(new Expression('IFNULL(categories.title, "яя")'));
     }), 'sort' => array('defaultOrder' => ['id' => SORT_DESC]), 'pagination' => ['pageSize' => $size]]);
     return $this->render('index', ['dataProvider' => $dataProvider, 'category' => $category]);
 }
 public function up()
 {
     $this->addColumn('categories', 'alias', $this->string());
     $this->createIndex('alias_unq_idex', 'categories', 'alias', true);
     $categories = \stronglab\productcatalog\models\Categories::find()->all();
     foreach ($categories as $model) {
         $model->save();
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Categories::find();
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'active' => !is_null($this->active) && $this->active !== 0 ? $this->active : Categories::ACTIVE_ON, 'parent_id' => $this->parent_id, 'category_local_id' => $this->category_local_id]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description]);
     return $dataProvider;
 }