ProductController.php
2.44 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace frontend\controllers;
use common\modules\product\models\Category;
use common\modules\product\models\Product;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* Products controller
*/
class ProductController extends Controller
{
/**
* Displays product list.
* @return string
*/
public function actionIndex($id)
{
$category = Category::find()
->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;
}
}