a8370482
Alexander Karnovsky
init project
|
1
2
3
4
|
<?php
namespace common\modules\product\models;
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
5
|
use common\behaviors\Slug;
|
a8370482
Alexander Karnovsky
init project
|
6
7
8
9
10
11
12
13
14
|
use common\modules\rubrication\models\TaxOption;
use Yii;
use common\modules\relation\relationBehavior;
use yii\db\ActiveQuery;
/**
* This is the model class for table "{{%product}}".
*
* @property string $name
|
b519af22
Karnovsky A
Base-product func...
|
15
|
* @property integer $brand_id
|
a8370482
Alexander Karnovsky
init project
|
16
|
* @property integer $product_id
|
b519af22
Karnovsky A
Base-product func...
|
17
18
19
20
21
|
* @property Category $category
* @property array $categories
* @property ProductVariant $variant
* @property ProductImage $image
* @property array $images
|
a8370482
Alexander Karnovsky
init project
|
22
23
24
|
*/
class Product extends \yii\db\ActiveRecord
{
|
14eadb86
Karnovsky A
Eager loading for...
|
25
|
/** @var array $_variants */
|
b519af22
Karnovsky A
Base-product func...
|
26
|
public $_variants = [];
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
27
28
29
|
/** @var array $_images */
public $imagesUpload = [];
|
a8370482
Alexander Karnovsky
init project
|
30
31
32
33
34
35
36
37
38
|
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => relationBehavior::className(),
'relations' => [
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
39
40
|
'product_categories' => 'entity1', // Product category
'product_option' => 'entity1' // Product category
|
a8370482
Alexander Karnovsky
init project
|
41
42
|
]
],
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
43
44
45
46
47
48
|
[
'class' => Slug::className(),
'in_attribute' => 'name',
'out_attribute' => 'alias',
'translit' => true
]
|
a8370482
Alexander Karnovsky
init project
|
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%product}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
|
b519af22
Karnovsky A
Base-product func...
|
66
|
[['brand_id'], 'integer'],
|
a8370482
Alexander Karnovsky
init project
|
67
|
[['name'], 'string', 'max' => 150],
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
68
69
|
[['alias'], 'string', 'max' => 250],
[['categories', 'variants', 'options'], 'safe'],
|
0cc2a675
Karnovsky A
Base-product#4 fu...
|
70
|
// [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif'],
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
71
|
[['description', 'video'], 'safe'],
|
a8370482
Alexander Karnovsky
init project
|
72
73
74
75
76
77
78
79
80
81
|
// [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
|
b519af22
Karnovsky A
Base-product func...
|
82
|
'product_id' => Yii::t('product', 'ID'),
|
a8370482
Alexander Karnovsky
init project
|
83
|
'name' => Yii::t('product', 'Name'),
|
b519af22
Karnovsky A
Base-product func...
|
84
|
'brand_id' => Yii::t('product', 'Brand'),
|
a8370482
Alexander Karnovsky
init project
|
85
|
'categories' => Yii::t('product', 'Categories'), // relation behavior field
|
b519af22
Karnovsky A
Base-product func...
|
86
|
'category' => Yii::t('product', 'Category'), // relation behavior field
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
87
88
|
'image' => Yii::t('product', 'Image'),
'images' => Yii::t('product', 'Images'),
|
a8370482
Alexander Karnovsky
init project
|
89
90
91
92
93
94
95
96
|
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBrand()
{
|
b519af22
Karnovsky A
Base-product func...
|
97
|
return $this->hasOne(Brand::className(), ['brand_id' => 'brand_id']);
|
a8370482
Alexander Karnovsky
init project
|
98
99
|
}
|
b519af22
Karnovsky A
Base-product func...
|
100
101
102
103
104
105
106
107
108
109
110
111
|
/**
* @return \yii\db\ActiveQuery
*/
public function getImage()
{
return $this->hasOne(ProductImage::className(), ['product_id' => 'product_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getImages()
|
a8370482
Alexander Karnovsky
init project
|
112
|
{
|
b519af22
Karnovsky A
Base-product func...
|
113
|
return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id']);
|
a8370482
Alexander Karnovsky
init project
|
114
115
|
}
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
116
117
118
119
|
public function setImages($images) {
$this->_images = $images;
}
|
b519af22
Karnovsky A
Base-product func...
|
120
121
122
123
|
/**
* @return \yii\db\ActiveQuery
*/
public function getVariant()
|
a8370482
Alexander Karnovsky
init project
|
124
|
{
|
b519af22
Karnovsky A
Base-product func...
|
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']);
}
public function getVariantPrice() {
return $this->variant->price;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getVariants()
{
return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']);
}
public function setVariants($variants) {
$this->_variants = $variants;
}
public function getFullName()
{
|
7cdb0894
Karnovsky A
Some fixes
|
145
|
return empty($this->brand) ? $this->name : $this->brand->name .' '. $this->name;
|
b519af22
Karnovsky A
Base-product func...
|
146
147
148
149
150
151
152
153
154
155
156
157
|
}
public function getCategories() {
return $this->getRelations('product_categories');
}
public function getCategoriesNames() {
$result = [];
foreach($this->categories as $category) {
$result[] = $category->name;
}
return $result;
|
a8370482
Alexander Karnovsky
init project
|
158
159
160
161
162
163
164
|
}
public function getCategory() {
/** @var ActiveQuery $categories */
$categories = $this->getRelations('product_categories');
$count = $categories->count();
if ($count == 0)
|
b519af22
Karnovsky A
Base-product func...
|
165
166
|
return;
return $categories->one();
|
a8370482
Alexander Karnovsky
init project
|
167
168
|
}
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
169
170
171
172
|
public function getOptions() {
return $this->getRelations('product_option');
}
|
a8370482
Alexander Karnovsky
init project
|
173
174
175
176
177
178
179
180
|
/**
* @inheritdoc
* @return ProductQuery the active query used by this AR class.
*/
public static function find()
{
return new ProductQuery(get_called_class());
}
|
b519af22
Karnovsky A
Base-product func...
|
181
182
183
184
185
|
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
|
0cc2a675
Karnovsky A
Base-product#4 fu...
|
186
187
188
|
// foreach($this->imagesUpload as $image) {
// $image->saveAs('/images/items/' . $image->baseName .'_'. uniqid() . '.' . $image->extension);
// }
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
189
|
|
b519af22
Karnovsky A
Base-product func...
|
190
191
192
193
194
|
$todel = [];
foreach ($this->variants ? : [] as $_variant) {
$todel[$_variant->product_variant_id] = $_variant->product_variant_id;
}
foreach ($this->_variants as $_variant) {
|
5aa7418e
Karnovsky A
Base-product#3 fu...
|
195
196
197
|
if (!is_array($_variant)) {
return;
}
|
b519af22
Karnovsky A
Base-product func...
|
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
if (!empty($_variant['product_variant_id'])) {
unset($todel[$_variant['product_variant_id']]);
$model = ProductVariant::findOne($_variant['product_variant_id']);
} else {
$model = new ProductVariant();
}
$_variant['product_id'] = $this->product_id;
$model->load(['ProductVariant' => $_variant]);
$model->save();
}
if (!empty($todel)) {
ProductVariant::deleteAll(['product_variant_id' => $todel]);
}
}
|
a8370482
Alexander Karnovsky
init project
|
212
|
}
|