CollectionController.php
2.49 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
<?php
namespace frontend\controllers;
use common\models\Project;
use common\modules\product\models\Product;
use common\modules\product\models\ProductVariant;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* Article controller
*/
class CollectionController extends Controller
{
/**
* Displays article list.
* @return string
*/
public function actionIndex($id)
{
/**
* @var ProductVariant $variant
*/
$collection = $this->findCollection($id);
$variants = $collection->variants;
$variant = current($variants);
return $this->redirect([
'view',
'collection_id' => $collection->product_id,
'variant_id' => $variant->product_variant_id,
]);
}
public function actionView($collection_id, $variant_id)
{
$collection = $this->findCollection($collection_id, $variant_id);
$variants = $collection->variants;
$variants = ArrayHelper::index($variants, 'product_variant_id');
$variant = $variants[ $variant_id ];
$projects = Project::find()
->joinWith('productToProject')
->with('image')
->where([ 'product_to_project.product_variant_id' => $variant->product_variant_id ])
->all();
return $this->render('view', [
'collection' => $collection,
'variants' => $variants,
'variant' => $variant,
'projects' => $projects,
]);
}
/**
* @param $id
*
* @return Product
* @throws NotFoundHttpException
*/
private function findCollection($id, $variant_id = NULL)
{
$model = Product::find()
->joinWith('variants', true, 'INNER JOIN')
->with('productSpec')
->where([ 'product.product_id' => $id ])
->andFilterWhere([ 'product_variant_id' => $variant_id ])
->one();
if(empty( $model )) {
throw new NotFoundHttpException();
}
return $model;
}
}