[ 'class' => LanguageBehavior::className(), ], 'timestamp' => [ 'class' => TimestampBehavior::className(), ], [ 'class' => ManyToManyBehavior::className(), ], 'defaultVariant' => [ 'class' => DefaultVariantBehavior::className(), ], [ 'class' => BitMaskBehavior::className(), 'fields' => [ 'top' => 0, 'new' => 1, 'akcia' => 2, ], ], [ 'class' => GalleryBehavior::className(), ], ]; } /** * @inheritdoc */ public function rules() { return [ [ [ 'brand_id', 'mask', 'sort', 'image_id', ], 'integer', ], [ [ 'video', 'gallery', ], 'string', ], [ [ 'status' ], 'boolean', ], [ [ 'brand_id' ], 'exist', 'skipOnError' => true, 'targetClass' => Brand::className(), 'targetAttribute' => [ 'brand_id' => 'id' ], ], [ [ 'productOptionCompls', 'productOptionExcls', ], 'safe', ], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => Yii::t('catalog', 'ID'), 'brand_id' => Yii::t('catalog', 'Brand ID'), 'video' => Yii::t('catalog', 'Video'), 'mask' => Yii::t('catalog', 'Mask'), 'status' => Yii::t('catalog', 'Status'), 'sort' => Yii::t('catalog', 'Sort'), 'created_at' => Yii::t('catalog', 'Created At'), 'updated_at' => Yii::t('catalog', 'Updated At'), 'image_id' => Yii::t('catalog', 'Image'), ]; } /** * @return \yii\db\ActiveQuery */ public function getBrand() { return $this->hasOne(Brand::className(), [ 'id' => 'brand_id' ]) ->inverseOf('products'); } /** * @return \yii\db\ActiveQuery */ public function getProductLangs() { return $this->hasMany(ProductLang::className(), [ 'product_id' => 'id' ]) ->inverseOf('product'); } /** * @return \yii\db\ActiveQuery */ public function getLanguages() { return $this->hasMany(Language::className(), [ 'id' => 'language_id' ]) ->viaTable('product_lang', [ 'product_id' => 'id' ]); } /** * @return \yii\db\ActiveQuery */ public function getProductToCategories() { return $this->hasMany(ProductToCategory::className(), [ 'product_id' => 'id' ]) ->inverseOf('product'); } /** * @return \yii\db\ActiveQuery */ public function getCategories() { return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) ->viaTable('product_to_category', [ 'product_id' => 'id' ]); } /** * @return ActiveQuery */ public function getCategory() { return $this->hasOne(Category::className(), [ 'id' => 'category_id' ]) ->viaTable('product_to_category', [ 'product_id' => 'id' ]); } /** * @return \yii\db\ActiveQuery */ public function getProductToImages() { return $this->hasMany(ProductToImage::className(), [ 'product_id' => 'id' ]) ->inverseOf('product'); } /** * @return \yii\db\ActiveQuery */ // public function getImages() // { // return $this->hasMany(Image::className(), [ 'id' => 'image_id' ]) // ->viaTable('product_to_image', [ 'product_id' => 'id' ]); // } /** * @return \yii\db\ActiveQuery */ public function getProductToProductOptionCompls() { return $this->hasMany(ProductToProductOptionCompl::className(), [ 'product_id' => 'id' ]) ->inverseOf('product'); } /** * @return \yii\db\ActiveQuery */ public function getProductOptionCompls() { return $this->hasMany(ProductOptionCompl::className(), [ 'id' => 'product_option_compl_id' ]) ->viaTable('product_to_product_option_compl', [ 'product_id' => 'id' ]); } /** * @return \yii\db\ActiveQuery */ public function getProductToProductOptionExcls() { return $this->hasMany(ProductToProductOptionExcl::className(), [ 'product_id' => 'id' ]) ->inverseOf('product'); } /** * @return \yii\db\ActiveQuery */ public function getProductOptionExcls() { return $this->hasMany(ProductOptionExcl::className(), [ 'id' => 'product_option_excl_id' ]) ->viaTable('product_to_product_option_excl', [ 'product_id' => 'id' ]); } /** * @return \yii\db\ActiveQuery */ public function getVariants() { return $this->hasMany(Variant::className(), [ 'product_id' => 'id' ]) ->inverseOf('product'); } public function getVariant() { return $this->hasOne(Variant::className(), [ 'product_id' => 'id' ]) ->inverseOf('product'); } /** * Setter for Product Options Complementary * * @param $value */ public function setProductOptionCompls($value) { $this->productOptionCompls = $value; } /** * Setter for Product Options Exclusion * * @param $value */ public function setProductOptionExcls($value) { $this->productOptionExcls = $value; } /** * @return \yii\db\ActiveQuery */ public function getImage() { return $this->hasOne(Image::className(), [ 'id' => 'image_id' ]); } public function getSimilarProducts(int $limit = 10) { $poc = ArrayHelper::getColumn($this->productToProductOptionCompls, 'product_option_compl_id'); $poe = ArrayHelper::getColumn($this->productToProductOptionExcls, 'product_option_excl_id'); $query_compl = self::find() ->with('variants.image', 'lang') ->with('variant', 'variant.variantOptionCompls', 'variant.image') ->with(['variant.variantOptionExcls' => function (ActiveQuery $query){ $query->with('group', 'group.lang', 'lang'); }]) ->with('category', 'category.lang') ->with('brand', 'brand.lang') ->innerJoinWith('productToCategories', false) ->joinWith('productToProductOptionCompls', false) ->with(['productOptionExcls' => function (ActiveQuery $query){ $query->with('group', 'group.lang', 'lang'); }]) ->with(['productOptionCompls' => function (ActiveQuery $query){ $query->with('group', 'group.lang', 'lang'); }]) ->andWhere([ 'product_to_product_option_compl.product_option_compl_id' => $poc ]) ->andWhere( [ 'not', [ 'product.id' => $this->id, ], ] ); $query_excl = self::find() ->with('variants.image', 'lang') ->with('brand', 'brand.lang') ->with('variant', 'variant.variantOptionCompls', 'variant.image') ->with(['variant.variantOptionExcls' => function (ActiveQuery $query){ $query->with('group', 'group.lang', 'lang'); }]) ->with(['productOptionCompls' => function (ActiveQuery $query){ $query->with('group', 'group.lang', 'lang'); }]) ->with('category', 'category.lang') ->innerJoinWith('productToCategories', false) ->joinWith('productToProductOptionExcls', false) ->with(['productOptionExcls' => function (ActiveQuery $query){ $query->with('group', 'group.lang', 'lang'); }]) ->andWhere([ 'product_to_product_option_excl.product_option_excl_id' => $poe ]) ->andWhere( [ 'not', [ 'product.id' => $this->id, ], ] ) ->limit(8); if (class_exists('\artbox\stock\models\VariantToShop')){ $query_compl->with('variant.counts'); $query_excl->with('variant.counts'); } $result = $query_compl->union($query_excl, false) ->limit($limit); return $result->all(); } /** * @return ActiveQuery */ public static function findWithFilters() { return self::find() ->with( [ 'productOptionCompls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ) ->with( [ 'productOptionExcls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ) ->with( [ 'variants' => function ($query) { /** * @var ActiveQuery $query */ $query->with( [ 'variantOptionCompls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ) ->with( [ 'variantOptionExcls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ) ->with('lang'); }, ] ); } /** * Get ProductRecommends * * @return ActiveQuery */ public function getProductRecommends() { return $this->hasMany(ProductRecommend::className(), [ 'product_id' => 'id' ]); } /** * Get recommended Products * * @return ActiveQuery */ public function getRecommendedProducts() { return $this->hasMany(Product::className(), [ 'id' => 'recommend_id' ]) ->via('productRecommends'); } public function getRelatedArticles() { if (class_exists('\artbox\weblog\models\Article')) { return $this->hasMany('\artbox\weblog\models\Article', [ 'id' => 'article_id' ])->viaTable('article_to_product', [ 'product_id' => 'id' ]); } else { return null; } } public function getEvent(){ return $this->hasMany(Event::className(), [ 'id' => 'event_id' ])->viaTable('product_to_event', [ 'product_id' => 'id' ]); } }