From adcaa25d57647f618c1b234426849e0982ff47b8 Mon Sep 17 00:00:00 2001 From: Yarik Date: Tue, 6 Jun 2017 15:17:00 +0300 Subject: [PATCH] Article to product begin --- migrations/catalog/m170606_114809_article_related_product.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ models/Article.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ models/ArticleToProduct.php | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 24 deletions(-) create mode 100644 migrations/catalog/m170606_114809_article_related_product.php create mode 100755 models/ArticleToProduct.php diff --git a/migrations/catalog/m170606_114809_article_related_product.php b/migrations/catalog/m170606_114809_article_related_product.php new file mode 100644 index 0000000..a73b2f8 --- /dev/null +++ b/migrations/catalog/m170606_114809_article_related_product.php @@ -0,0 +1,50 @@ +createTable( + 'article_to_product', + [ + 'article_id' => $this->integer() + ->notNull(), + 'product_id' => $this->integer() + ->notNull(), + ] + ); + $this->addPrimaryKey( + 'article_to_product_pk', + 'article_to_product', + [ + 'article_id', + 'product_id', + ] + ); + $this->addForeignKey( + 'article_id_fkey', + 'article_to_product', + 'article_id', + 'blog_article', + 'id', + 'CASCADE', + 'CASCADE' + ); + $this->addForeignKey( + 'product_id_fkey', + 'article_to_product', + 'product_id', + 'product', + 'id', + 'CASCADE', + 'CASCADE' + ); + } + + public function safeDown() + { + $this->dropTable('article_to_product'); + } + } diff --git a/models/Article.php b/models/Article.php index 1c2ecd4..ad115e0 100755 --- a/models/Article.php +++ b/models/Article.php @@ -10,36 +10,38 @@ use artbox\core\behaviors\LanguageBehavior; use artbox\core\models\Language; use yii\db\ActiveQuery; + use yii\db\Query; use yii\web\Request; /** * This is the model class for table "blog_article". * - * @property integer $id - * @property Image $image - * @property integer $created_at - * @property integer $updated_at - * @property integer $deleted_at - * @property integer $sort - * @property boolean $status - * @property integer $author_id - * @property integer $image_id - * @property ArticleLang[] $blogArticleLangs - * @property Language[] $languages - * @property Article[] $relatedBlogArticles - * @property Article[] $articles - * @property Category[] $categories - * @property Category $category - * @property Product[] $products - * @property Tag[] $tags + * @property integer $id + * @property Image $image + * @property integer $created_at + * @property integer $updated_at + * @property integer $deleted_at + * @property integer $sort + * @property boolean $status + * @property integer $author_id + * @property integer $image_id + * @property ArticleLang[] $blogArticleLangs + * @property Language[] $languages + * @property Article[] $relatedBlogArticles + * @property Article[] $articles + * @property Category[] $categories + * @property Category $category + * @property Product[] $products + * @property Tag[] $tags + * @property \artbox\catalog\models\Product $relatedProducts * * * From language behavior * - * @property ArticleLang $lang - * @property ArticleLang[] $langs - * @property ArticleLang $objectLang - * @property string $ownerKey - * @property string $langKey - * @property ArticleLang[] $modelLangs - * @property bool $transactionStatus + * @property ArticleLang $lang + * @property ArticleLang[] $langs + * @property ArticleLang $objectLang + * @property string $ownerKey + * @property string $langKey + * @property ArticleLang[] $modelLangs + * @property bool $transactionStatus * @method string getOwnerKey() * @method void setOwnerKey( string $value ) * @method string getLangKey() @@ -137,6 +139,31 @@ } /** + * @return Query + */ + public function getRelatedProducts() + { + if (class_exists('\artbox\catalog\models\Product')) { + return $this->hasMany('\artbox\catalog\models\Product', [ 'id' => 'article_id' ]) + ->via('articleToProduct'); + } else { + return ( new Query() )->where('1 = 0'); + } + } + + /** + * @return Query + */ + public function getArticleToProduct() + { + if (class_exists('\artbox\catalog\models\Product')) { + return $this->hasMany(ArticleToProduct::className(), [ 'id' => 'article_id' ]); + } else { + return ( new Query() )->where('1 = 0'); + } + } + + /** * @return \yii\db\ActiveQuery */ public function getImage() diff --git a/models/ArticleToProduct.php b/models/ArticleToProduct.php new file mode 100755 index 0000000..d587f43 --- /dev/null +++ b/models/ArticleToProduct.php @@ -0,0 +1,94 @@ + '\artbox\weblog\models\Article', + 'targetAttribute' => 'id', + ], + [ + [ + 'product_id', + ], + 'exist', + 'targetClass' => '\artbox\catalog\models\Product', + 'targetAttribute' => 'id', + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'article_id' => \Yii::t('blog', 'Article ID'), + 'product_id' => \Yii::t('blog', 'Product ID'), + ]; + } + + /** + * Get article query + * + * @return \yii\db\ActiveQuery + */ + public function getArticle() + { + return $this->hasOne('\artbox\weblog\models\Article', [ 'id' => 'article_id' ]); + } + + /** + * Get product query + * + * @return \yii\db\ActiveQuery + */ + public function getProduct() + { + return $this->hasOne('\artbox\catalog\models\Article', [ 'id' => 'product_id' ]); + } + } -- libgit2 0.21.4