diff --git a/models/Product.php b/models/Product.php index 980b0d3..e85613b 100755 --- a/models/Product.php +++ b/models/Product.php @@ -2,10 +2,11 @@ namespace artweb\artbox\ecommerce\models; + use artweb\artbox\comment\models\CommentModel; + use artweb\artbox\comment\models\interfaces\RatingCacheInterface; use artweb\artbox\ecommerce\behaviors\DefaultVariantBehavior; use artweb\artbox\behaviors\MultipleImgBehavior; use artweb\artbox\behaviors\SaveMultipleFileBehavior; -// use artweb\artbox\comment\models\CommentModel; use artweb\artbox\language\behaviors\LanguageBehavior; use Yii; use yii\base\InvalidParamException; @@ -76,6 +77,22 @@ * @method array getImagesConfig() * @method array getImagesHTML( string $preset ) * * End multipleImage behavior + * * From CommentBehavior ** + * @property string $cacheModelName + * @property RatingCacheInterface $cacheModel + * @property boolean $cacheRating + * @property CommentModel[] $comments + * @property RatingCacheInterface $averageRating + * @method string getCacheModelName() + * @method void setCacheModelName( string $value ) + * @method RatingCacheInterface getCacheModel() + * @method void setCacheModel( RatingCacheInterface $value ) + * @method boolean getCacheRating() + * @method void setCacheRating( bool $value ) + * @method ActiveQuery getComments() + * @method ActiveQuery getAverageRating() + * @method bool recalculateRating() + * * End CommentBehavior ** */ class Product extends ActiveRecord { @@ -88,7 +105,7 @@ public function behaviors() { return [ - 'images' => [ + 'images' => [ 'class' => SaveMultipleFileBehavior::className(), 'name' => 'imagesUpload', 'directory' => 'products', @@ -98,7 +115,7 @@ ], 'model' => ProductImage::className(), ], - 'multipleImage' => [ + 'multipleImage' => [ 'class' => MultipleImgBehavior::className(), 'links' => [ 'product_id' => 'id', @@ -113,10 +130,15 @@ 'id' => 'id', ], ], - 'language' => [ + 'language' => [ 'class' => LanguageBehavior::className(), ], 'defaultVariant' => DefaultVariantBehavior::className(), + 'comment' => [ + 'class' => 'artweb\artbox\comment\behaviors\CommentBehavior', + 'cacheRating' => true, + 'cacheModelName' => ProductToRating::className(), + ], ]; } @@ -490,70 +512,6 @@ } /** - * Recalculate rating for artboxcomment module - * - * @todo Rewrite with behavior - * @return bool - */ -// public function recalculateRating():bool -// { -// /** -// * @var ProductToRating $averageRating -// */ -// $average = $this->getComments() -// ->joinWith('rating') -// ->select([ 'average' => 'avg(artbox_comment_rating.value)::float' ]) -// ->scalar(); -// if (!$average) { -// $average = 0; -// } -// $averageRating = $this->averageRating; -// if (!empty( $averageRating )) { -// $averageRating->value = $average; -// } else { -// $averageRating = new ProductToRating( -// [ -// 'product_id' => $this->id, -// 'value' => $average, -// ] -// ); -// } -// if ($averageRating->save()) { -// return true; -// } else { -// return false; -// } -// } - - /** - * Get CommmentModel query for artboxcomment module - * - * @todo Rewrite with behavior - * @return ActiveQuery - */ -// public function getComments() -// { -// return $this->hasMany(CommentModel::className(), [ 'entity_id' => 'id' ]) -// ->where( -// [ -// 'artbox_comment.entity' => self::className(), -// 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, -// 'artbox_comment.artbox_comment_pid' => null, -// ] -// ); -// } - - /** - * Get ProductToRating query in order to get average rating for current Product - * - * @return \yii\db\ActiveQuery - */ -// public function getAverageRating() -// { -// return $this->hasOne(ProductToRating::className(), [ 'product_id' => 'id' ]); -// } - - /** * Get TaxGroupToCategories query via product_category table * * @return ActiveQuery diff --git a/models/ProductToRating.php b/models/ProductToRating.php new file mode 100644 index 0000000..820cfcb --- /dev/null +++ b/models/ProductToRating.php @@ -0,0 +1,102 @@ + true, + 'targetClass' => Product::className(), + 'targetAttribute' => [ 'product_id' => 'id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'product_id' => Yii::t('app', 'Product ID'), + 'value' => Yii::t('app', 'Value'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProduct() + { + return $this->hasOne(Product::className(), [ 'id' => 'product_id' ]); + } + + /** + * @inheritdoc + */ + public function getLinkAttribute(): string + { + return 'product_id'; + } + + /** + * @inheritdoc + */ + public function getValue(): float + { + return $this->value; + } + + /** + * @inheritdoc + */ + public function setValue(float $value) + { + $this->value = $value; + } + } -- libgit2 0.21.4