Commit adcaa25d57647f618c1b234426849e0982ff47b8
1 parent
2caa1ca3
Article to product begin
Showing
3 changed files
with
195 additions
and
24 deletions
Show diff stats
migrations/catalog/m170606_114809_article_related_product.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | + use yii\db\Migration; | |
| 4 | + | |
| 5 | + class m170606_114809_article_related_product extends Migration | |
| 6 | + { | |
| 7 | + public function safeUp() | |
| 8 | + { | |
| 9 | + $this->createTable( | |
| 10 | + 'article_to_product', | |
| 11 | + [ | |
| 12 | + 'article_id' => $this->integer() | |
| 13 | + ->notNull(), | |
| 14 | + 'product_id' => $this->integer() | |
| 15 | + ->notNull(), | |
| 16 | + ] | |
| 17 | + ); | |
| 18 | + $this->addPrimaryKey( | |
| 19 | + 'article_to_product_pk', | |
| 20 | + 'article_to_product', | |
| 21 | + [ | |
| 22 | + 'article_id', | |
| 23 | + 'product_id', | |
| 24 | + ] | |
| 25 | + ); | |
| 26 | + $this->addForeignKey( | |
| 27 | + 'article_id_fkey', | |
| 28 | + 'article_to_product', | |
| 29 | + 'article_id', | |
| 30 | + 'blog_article', | |
| 31 | + 'id', | |
| 32 | + 'CASCADE', | |
| 33 | + 'CASCADE' | |
| 34 | + ); | |
| 35 | + $this->addForeignKey( | |
| 36 | + 'product_id_fkey', | |
| 37 | + 'article_to_product', | |
| 38 | + 'product_id', | |
| 39 | + 'product', | |
| 40 | + 'id', | |
| 41 | + 'CASCADE', | |
| 42 | + 'CASCADE' | |
| 43 | + ); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public function safeDown() | |
| 47 | + { | |
| 48 | + $this->dropTable('article_to_product'); | |
| 49 | + } | |
| 50 | + } | ... | ... |
models/Article.php
| ... | ... | @@ -10,36 +10,38 @@ |
| 10 | 10 | use artbox\core\behaviors\LanguageBehavior; |
| 11 | 11 | use artbox\core\models\Language; |
| 12 | 12 | use yii\db\ActiveQuery; |
| 13 | + use yii\db\Query; | |
| 13 | 14 | use yii\web\Request; |
| 14 | 15 | |
| 15 | 16 | /** |
| 16 | 17 | * This is the model class for table "blog_article". |
| 17 | 18 | * |
| 18 | - * @property integer $id | |
| 19 | - * @property Image $image | |
| 20 | - * @property integer $created_at | |
| 21 | - * @property integer $updated_at | |
| 22 | - * @property integer $deleted_at | |
| 23 | - * @property integer $sort | |
| 24 | - * @property boolean $status | |
| 25 | - * @property integer $author_id | |
| 26 | - * @property integer $image_id | |
| 27 | - * @property ArticleLang[] $blogArticleLangs | |
| 28 | - * @property Language[] $languages | |
| 29 | - * @property Article[] $relatedBlogArticles | |
| 30 | - * @property Article[] $articles | |
| 31 | - * @property Category[] $categories | |
| 32 | - * @property Category $category | |
| 33 | - * @property Product[] $products | |
| 34 | - * @property Tag[] $tags | |
| 19 | + * @property integer $id | |
| 20 | + * @property Image $image | |
| 21 | + * @property integer $created_at | |
| 22 | + * @property integer $updated_at | |
| 23 | + * @property integer $deleted_at | |
| 24 | + * @property integer $sort | |
| 25 | + * @property boolean $status | |
| 26 | + * @property integer $author_id | |
| 27 | + * @property integer $image_id | |
| 28 | + * @property ArticleLang[] $blogArticleLangs | |
| 29 | + * @property Language[] $languages | |
| 30 | + * @property Article[] $relatedBlogArticles | |
| 31 | + * @property Article[] $articles | |
| 32 | + * @property Category[] $categories | |
| 33 | + * @property Category $category | |
| 34 | + * @property Product[] $products | |
| 35 | + * @property Tag[] $tags | |
| 36 | + * @property \artbox\catalog\models\Product $relatedProducts | |
| 35 | 37 | * * * From language behavior * |
| 36 | - * @property ArticleLang $lang | |
| 37 | - * @property ArticleLang[] $langs | |
| 38 | - * @property ArticleLang $objectLang | |
| 39 | - * @property string $ownerKey | |
| 40 | - * @property string $langKey | |
| 41 | - * @property ArticleLang[] $modelLangs | |
| 42 | - * @property bool $transactionStatus | |
| 38 | + * @property ArticleLang $lang | |
| 39 | + * @property ArticleLang[] $langs | |
| 40 | + * @property ArticleLang $objectLang | |
| 41 | + * @property string $ownerKey | |
| 42 | + * @property string $langKey | |
| 43 | + * @property ArticleLang[] $modelLangs | |
| 44 | + * @property bool $transactionStatus | |
| 43 | 45 | * @method string getOwnerKey() |
| 44 | 46 | * @method void setOwnerKey( string $value ) |
| 45 | 47 | * @method string getLangKey() |
| ... | ... | @@ -137,6 +139,31 @@ |
| 137 | 139 | } |
| 138 | 140 | |
| 139 | 141 | /** |
| 142 | + * @return Query | |
| 143 | + */ | |
| 144 | + public function getRelatedProducts() | |
| 145 | + { | |
| 146 | + if (class_exists('\artbox\catalog\models\Product')) { | |
| 147 | + return $this->hasMany('\artbox\catalog\models\Product', [ 'id' => 'article_id' ]) | |
| 148 | + ->via('articleToProduct'); | |
| 149 | + } else { | |
| 150 | + return ( new Query() )->where('1 = 0'); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * @return Query | |
| 156 | + */ | |
| 157 | + public function getArticleToProduct() | |
| 158 | + { | |
| 159 | + if (class_exists('\artbox\catalog\models\Product')) { | |
| 160 | + return $this->hasMany(ArticleToProduct::className(), [ 'id' => 'article_id' ]); | |
| 161 | + } else { | |
| 162 | + return ( new Query() )->where('1 = 0'); | |
| 163 | + } | |
| 164 | + } | |
| 165 | + | |
| 166 | + /** | |
| 140 | 167 | * @return \yii\db\ActiveQuery |
| 141 | 168 | */ |
| 142 | 169 | public function getImage() | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | + namespace artbox\weblog\models; | |
| 4 | + | |
| 5 | + use yii\db\ActiveRecord; | |
| 6 | + | |
| 7 | + /** | |
| 8 | + * This is the model class for table "article_to_product". | |
| 9 | + * | |
| 10 | + * @property integer $article_id | |
| 11 | + * @property integer $product_id | |
| 12 | + * @property \artbox\weblog\models\Article $article | |
| 13 | + * @property \artbox\catalog\models\Product $product | |
| 14 | + */ | |
| 15 | + class ArticleToProduct extends ActiveRecord | |
| 16 | + { | |
| 17 | + /** | |
| 18 | + * @inheritdoc | |
| 19 | + */ | |
| 20 | + public static function tableName() | |
| 21 | + { | |
| 22 | + return 'article_to_product'; | |
| 23 | + } | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * @inheritdoc | |
| 27 | + */ | |
| 28 | + public function rules() | |
| 29 | + { | |
| 30 | + return [ | |
| 31 | + [ | |
| 32 | + [ | |
| 33 | + 'article_id', | |
| 34 | + 'product_id', | |
| 35 | + ], | |
| 36 | + 'integer', | |
| 37 | + ], | |
| 38 | + [ | |
| 39 | + [ | |
| 40 | + 'article_id', | |
| 41 | + 'product_id', | |
| 42 | + ], | |
| 43 | + 'required', | |
| 44 | + ], | |
| 45 | + [ | |
| 46 | + [ | |
| 47 | + 'article_id', | |
| 48 | + ], | |
| 49 | + 'exist', | |
| 50 | + 'targetClass' => '\artbox\weblog\models\Article', | |
| 51 | + 'targetAttribute' => 'id', | |
| 52 | + ], | |
| 53 | + [ | |
| 54 | + [ | |
| 55 | + 'product_id', | |
| 56 | + ], | |
| 57 | + 'exist', | |
| 58 | + 'targetClass' => '\artbox\catalog\models\Product', | |
| 59 | + 'targetAttribute' => 'id', | |
| 60 | + ], | |
| 61 | + ]; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * @inheritdoc | |
| 66 | + */ | |
| 67 | + public function attributeLabels() | |
| 68 | + { | |
| 69 | + return [ | |
| 70 | + 'article_id' => \Yii::t('blog', 'Article ID'), | |
| 71 | + 'product_id' => \Yii::t('blog', 'Product ID'), | |
| 72 | + ]; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Get article query | |
| 77 | + * | |
| 78 | + * @return \yii\db\ActiveQuery | |
| 79 | + */ | |
| 80 | + public function getArticle() | |
| 81 | + { | |
| 82 | + return $this->hasOne('\artbox\weblog\models\Article', [ 'id' => 'article_id' ]); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Get product query | |
| 87 | + * | |
| 88 | + * @return \yii\db\ActiveQuery | |
| 89 | + */ | |
| 90 | + public function getProduct() | |
| 91 | + { | |
| 92 | + return $this->hasOne('\artbox\catalog\models\Article', [ 'id' => 'product_id' ]); | |
| 93 | + } | |
| 94 | + } | ... | ... |