diff --git a/controllers/BrandSizeController.php b/controllers/BrandSizeController.php index 0e6edb4..c9892b3 100755 --- a/controllers/BrandSizeController.php +++ b/controllers/BrandSizeController.php @@ -8,6 +8,7 @@ use artweb\artbox\ecommerce\models\BrandSize; use artweb\artbox\ecommerce\models\BrandSizeSearch; use yii\helpers\ArrayHelper; + use yii\helpers\VarDumper; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; @@ -126,7 +127,7 @@ public function actionUpdate($id) { $model = $this->findModel($id); - + $categories = ArrayHelper::map( Category::find() ->with('lang') diff --git a/models/BrandSize.php b/models/BrandSize.php index d13a07e..fd75ba8 100755 --- a/models/BrandSize.php +++ b/models/BrandSize.php @@ -69,7 +69,7 @@ class BrandSize extends \yii\db\ActiveRecord */ public function getBrand() { - return $this->hasOne(Brand::className(), ['id' => 'brand_id']); + return $this->hasOne(Brand::className(), ['id' => 'brand_id'])->with('lang'); } /** diff --git a/models/Product.php b/models/Product.php index da3077a..be3de9a 100755 --- a/models/Product.php +++ b/models/Product.php @@ -105,7 +105,7 @@ class Product extends ActiveRecord { public $option_id; - + public $min; public $max; @@ -283,20 +283,17 @@ { return $this->getVariant(); } - - - - - + /** * @return \yii\db\ActiveQuery */ - public function getEvents(){ - return $this->hasMany(Event::className(), ['id' => 'event_id'])->viaTable('events_to_products', ['product_id' => 'id'])->where(['status'=>Event::ACTIVE]); + public function getEvents() + { + return $this->hasMany(Event::className(), [ 'id' => 'event_id' ]) + ->viaTable('events_to_products', [ 'product_id' => 'id' ]) + ->where([ 'status' => Event::ACTIVE ]); } - - - + /** * Get ProductVariant query fetching only available in stock to current Product * @@ -332,21 +329,20 @@ ] ); } - - - public function getMaxPrice(){ + + public function getMaxPrice() + { $price = 0; - if (!empty( $this->enabledVariants )) { - foreach ( $this->enabledVariants as $variant){ - if($variant->price > $price){ + if (!empty($this->enabledVariants)) { + foreach ($this->enabledVariants as $variant) { + if ($variant->price > $price) { $price = $variant->price; } } } return $price; } - - + /** * Get random ProductVariant price or 0 if not exist * @@ -357,7 +353,7 @@ */ public function getVariantPrice(bool $exception = false): float { - if (!empty( $this->variant )) { + if (!empty($this->variant)) { return $this->variant->price; } elseif ($exception) { throw new NotFoundHttpException('Product with ID ' . $this->id . ' hasn\'t got variants'); @@ -376,7 +372,7 @@ */ public function getEnabledVariantPrice(bool $exception = false): float { - if (!empty( $this->enabledVariant )) { + if (!empty($this->enabledVariant)) { return $this->enabledVariant->price; } elseif ($exception) { throw new NotFoundHttpException('Product with ID ' . $this->id . ' hasn\'t got enabled variants'); @@ -384,39 +380,53 @@ return 0; } } - /** * Get Product name concatenated with Brand name * * @return string */ - public function getFullName():string + public function getFullName(): string { $name = ''; - $groupName = (new Query())->select(['tax_option.*','tax_option_lang.*']) - ->from([ 'tax_option' ]) - ->innerJoin('tax_group', 'tax_group.id = tax_option.tax_group_id') - ->innerJoin('tax_option_lang', 'tax_option.id = tax_option_lang.tax_option_id') - ->innerJoin('tax_group_to_category', 'tax_group.id = tax_group_to_category.tax_group_id') - ->where(['tax_group_to_category.category_id' => $this->category->id, 'tax_group.use_in_name'=>1, 'tax_option.id' => ArrayHelper::getColumn($this->options,'id')]) - ->one(); - - - if($groupName != null ){ - $groupName = $groupName['value']; + $groupName = ( new Query() )->select( + [ + 'tax_option.*', + 'tax_option_lang.*', + ] + ) + ->from([ 'tax_option' ]) + ->innerJoin('tax_group', 'tax_group.id = tax_option.tax_group_id') + ->innerJoin('tax_option_lang', 'tax_option.id = tax_option_lang.tax_option_id') + ->innerJoin( + 'tax_group_to_category', + 'tax_group.id = tax_group_to_category.tax_group_id' + ) + ->where( + [ + 'tax_group_to_category.category_id' => $this->category->id, + 'tax_group.use_in_name' => 1, + 'tax_option.id' => ArrayHelper::getColumn( + $this->options, + 'id' + ), + ] + ) + ->one(); + + if ($groupName != NULL) { + $groupName = $groupName[ 'value' ]; } else { $groupName = ''; } - - - if (!empty( $this->category->lang->category_synonym )) { - $name = $name . ($groupName ? $groupName : $this->category->lang->category_synonym) . ' '; + + if (!empty($this->category->lang->category_synonym)) { + $name = $name . ( $groupName ? $groupName : $this->category->lang->category_synonym ) . ' '; } else { - $name = $name . ($groupName ? $groupName : $this->category->lang->title) . ' '; + $name = $name . ( $groupName ? $groupName : $this->category->lang->title ) . ' '; } - - if (!empty( $this->brand )) { + + if (!empty($this->brand)) { $name = $name . $this->brand->lang->title . ' '; } $name .= $this->lang->title; @@ -485,11 +495,10 @@ public function getOptions() { return $this->hasMany(TaxOption::className(), [ 'id' => 'option_id' ]) - ->viaTable('product_option', [ 'product_id' => 'id' ]); - + ->viaTable('product_option', [ 'product_id' => 'id' ]); + } - - + /** * Get TaxOptions query for current Product that will by used in filter * @@ -497,13 +506,17 @@ */ public function getFilterOptions() { - + return $this->getOptions() - ->joinWith(['lang','taxGroup']) - ->where(['is_filter' => true]); + ->joinWith( + [ + 'lang', + 'taxGroup', + ] + ) + ->where([ 'is_filter' => true ]); } - - + /** * Get TaxGroup for current Product * @@ -511,11 +524,10 @@ */ public function getGroups() { - return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ]) - ->via('options'); + return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ]) + ->via('options'); } - - + /** * Get TaxOptions query for current Product joined with TaxGroups * @@ -552,7 +564,7 @@ /** * @var TaxGroup $group */ - if (!empty( $options[ $group->id ] )) { + if (!empty($options[ $group->id ])) { $group->customOptions = $options[ $group->id ]; $groups[] = $group; } @@ -593,7 +605,7 @@ * @see Product::getProductStocks() * @return int */ - public function getQuantity():int + public function getQuantity(): int { return $this->getProductStocks() ->sum('quantity'); @@ -607,10 +619,8 @@ public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); - - - - if (!empty( $this->categories )) { + + if (!empty($this->categories)) { $categories = Category::findAll($this->categories); $this->unlinkAll('categories', true); foreach ($categories as $category) { @@ -618,7 +628,7 @@ } } - if (!empty( $this->options )) { + if (!empty($this->options)) { $options = TaxOption::findAll($this->options); $this->unlinkAll('options', true); foreach ($options as $option) { @@ -664,7 +674,20 @@ public function getSize() { - $size = BrandSize::find()->joinWith('categories')->createCommand()->rawSql; + $subQuery = Product::find() + ->select('category.id as x') + ->joinWith('categories') + ->where([ 'product.id' => $this->id, ]); + $size = BrandSize::find() + ->joinWith('categories') + ->innerJoin([ 's' => $subQuery ], 's.x = category.id') + ->where( + [ + 'brand_size.brand_id' => $this->brand->id, + ] + ) + ->one(); + return $size; } /** @@ -686,17 +709,17 @@ { $this->options = $values; } - + public function beforeSave($insert) { if (parent::beforeSave($insert)) { - + return true; } else { return false; } } - + public function beforeDelete() { if (parent::beforeDelete()) { diff --git a/views/brand-size/index.php b/views/brand-size/index.php index 57b33e7..0bbd456 100755 --- a/views/brand-size/index.php +++ b/views/brand-size/index.php @@ -25,8 +25,8 @@ $this->params['breadcrumbs'][] = $this->title; ['class' => 'yii\grid\SerialColumn'], 'id', - 'brand_id', - 'image', + 'brand.lang.title', + 'imageUrl:image', ['class' => 'yii\grid\ActionColumn'], ], diff --git a/views/brand-size/view.php b/views/brand-size/view.php index 97de248..d85894f 100755 --- a/views/brand-size/view.php +++ b/views/brand-size/view.php @@ -51,8 +51,8 @@ 'model' => $model, 'attributes' => [ 'id', - 'brand_id', - 'image', + 'brand.lang.title', + 'imageUrl:image', ], ] ) ?> -- libgit2 0.21.4