where([ 'category_id' => $id, ])->one(); $query = Product::find() ->joinWith(['categories.grandParent c3']) ->with('variant') ->where(['c3.category_id' => $id]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 15, ], ]); return $this->render('index', [ 'dataProvider' => $dataProvider, 'category' => $category, ]); } /** * @param int $product_id * @param int $variant_id * @return string */ public function actionView($product_id, $variant_id) { $product = Product::find() ->joinWith([ 'variants' => function($query) { $query->indexBy('product_variant_id'); } ], true, 'INNER JOIN') ->where([ 'product.product_id' => $product_id, 'product_variant.product_variant_id' => $variant_id, ])->with('category.parent')->one(); $variant = $product->variants[$variant_id]; $variants = $product->variants; return $this->render('view', [ 'variants' => $variants, 'product' => $product, 'variant' => $variant, ]); } /** * @param int $id * @return Product * @throws NotFoundHttpException */ private function findProduct($id) { $model = Product::find()->where(['product_id' => (int) $id]) ->one(); if(empty($model)) { throw new NotFoundHttpException(); } return $model; } }