Blame view

app/models/Products.php 4.04 KB
bf807468   Alex Savenko   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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  <?php

  

  namespace app\models;

  use app\models\Fasovka;

  use app\models\Type;

  use app\models\Brends;

  

  class Products extends \yii\db\ActiveRecord

  {

      public $fasovka;

      public $type;

      public $brends;

      

      public static function tableName()

      {

          return 'products';

      }

      

  	public function rules()

  	{

  		return [

  			[['fasovka','type','brends'], 'safe'],

                      ];

  	}    

      

  	public function attributeLabels()

  	{

  		return [

  			'fasovka'=>'Фасовка',

                          'type'=>'Типы',

                          'brends'=>'Бренды',

  		];

  	}

      

      public function getCatalog()

          {

              return $this->hasOne(Catalog::className(), ['id' => 'catalog_id']);

          }         

          public function getMods()

          {

              return $this->hasMany(Mod::className(), ['product_id' => 'id']);

          }

          public function getMinCost()

          {

              return $this->hasOne(Mod::className(), ['product_id' => 'id'])->orderBy('cost');

  

          }

          public function getCost()

          {

              //$res = $this->hasOne(Mod::className(), ['product_id' => 'id'])->orderBy('cost');

              //print_r($res);exit;

              $res = Mod::find()->where(['product_id' => $this->id])->orderBy('cost')->one();

              return (!empty($res->cost)) ? $res : ((object) ['cost'=>0]);

          }

          public function getFasovka()

          {

              return $this->hasMany(ProductsFasovka::className(), ['product_id' => 'id']);

          }

          public function getType()

          {

              return $this->hasMany(ProductsType::className(), ['product_id' => 'id']);

          }

          public function getBrends()

          {

              return $this->hasMany(ProductsBrends::className(), ['product_id' => 'id']);

          }        

          public function getFotos()

          {

              return $this->hasMany(Fotos::className(), ['product_id' => 'id']);

          }

          

          

          

          public function fasovkaAll($catalog_id){

              $fasovka = Fasovka::find()->where(['catalog_id'=>$catalog_id])->innerJoinWith(['productsFasovka']);

               if(!empty($this->type)){

                 $fasovka->leftJoin('productsType', 'productsType.product_id = productsFasovka.product_id')->andWhere(['productsType.type_id'=>$this->type]);

               }

               if(!empty($this->brends)){

                 $fasovka->leftJoin('productsBrends', 'productsBrends.product_id = productsFasovka.product_id')->andWhere(['productsBrends.brend_id'=>$this->brends]);

               }

               return $fasovka->asArray()->orderBy('name')->all();

          }

          

          public function typeAll($catalog_id){

              $type = Type::find()->where(['catalog_id'=>$catalog_id])->innerJoinWith(['productsType']);

                  if(!empty($this->fasovka)){

                      $type->leftJoin('productsFasovka', 'productsFasovka.product_id = productsType.product_id')->andWhere(['productsFasovka.fasovka_id'=>$this->fasovka]);

                  }

                  if(!empty($this->brends)){

                      $type->leftJoin('productsBrends', 'productsBrends.product_id = productsType.product_id')->andWhere(['productsBrends.brend_id'=>$this->brends]);

                  } 

               return $type->asArray()->orderBy('name')->all();

          } 

          

          public function brendsAll($catalog_id){

              $brends = Brends::find()->where(['catalog_id'=>$catalog_id])->innerJoinWith(['productsBrends']);

                  if(!empty($this->fasovka)){

                      $brends->leftJoin('productsFasovka', 'productsFasovka.product_id = productsBrends.product_id')->andWhere(['productsFasovka.fasovka_id'=>$this->fasovka]);

                  }

                  if(!empty($this->type)){

                      $brends->leftJoin('productsType', 'productsType.product_id = productsBrends.product_id')->andWhere(['productsType.type_id'=>$this->type]);

                  }  

               return $brends->asArray()->orderBy('name')->all();

          }        

        

  }