Blame view

frontend/controllers/ProductController.php 1.75 KB
4ca21c3e   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  <?php
      
      namespace frontend\controllers;
      
      use common\modules\product\models\Product;
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
  
      /**
       * Products controller
       */
      class ProductController extends Controller
      {
          
          /**
           * Displays product list.
           * @return string
           */
          public function actionIndex()
          {
              return $this->render('index');
          }
  
          /**
           * @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;
          }
      }