From d7bd572580bc55ddd62ed43a113d21a940b62496 Mon Sep 17 00:00:00 2001 From: Yarik Date: Tue, 6 Jun 2017 16:16:08 +0300 Subject: [PATCH] Blog article to product --- controllers/ArticleController.php | 33 ++++++++++++++++++++++++++++++--- migrations/m161101_144434_blog_article_to_product.php | 60 ------------------------------------------------------------ models/Article.php | 65 ++++++++++++++++++++++++++++------------------------------------- views/blog-article/_form.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 100 deletions(-) delete mode 100755 migrations/m161101_144434_blog_article_to_product.php diff --git a/controllers/ArticleController.php b/controllers/ArticleController.php index 9f7b072..11f63d7 100755 --- a/controllers/ArticleController.php +++ b/controllers/ArticleController.php @@ -155,13 +155,21 @@ 'lang.title' ); + if (class_exists('\artbox\catalog\models\Product')) { + $model->productIds = ArrayHelper::map( + $model->relatedProducts, + 'id', + 'lang.title' + ); + } + if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { $categories = Category::find() ->where([ 'id' => \Yii::$app->request->post('categoryIds') ]) ->all(); - + $model->linkMany('categories', $categories); - + $tags = Tag::find() ->where( [ @@ -169,9 +177,28 @@ ] ) ->all(); - + $model->linkMany('tags', $tags); + + if (class_exists('\artbox\catalog\models\Product')) { + /** + * @var \yii\db\ActiveQuery $query + */ + $query = call_user_func( + [ + '\artbox\catalog\models\Product', + 'find', + ] + ); + /** + * @var \artbox\catalog\models\Product[] $products + */ + $products = $query->where([ 'id' => \Yii::$app->request->post('productIds') ]) + ->all(); + $model->linkMany('relatedProducts', $products); + } + return $this->redirect( [ 'view', diff --git a/migrations/m161101_144434_blog_article_to_product.php b/migrations/m161101_144434_blog_article_to_product.php deleted file mode 100755 index 78e1ed0..0000000 --- a/migrations/m161101_144434_blog_article_to_product.php +++ /dev/null @@ -1,60 +0,0 @@ -createTable( - 'blog_article_to_product', - [ - 'blog_article_id' => $this->integer() - ->notNull(), - 'product_id' => $this->integer() - ->notNull(), - ] - ); - - $this->createIndex( - 'blog_article_to_product_uk', - 'blog_article_to_product', - [ - 'blog_article_id', - 'product_id', - ], - true - ); - - $this->addForeignKey( - 'blog_article_to_product_art_fk', - 'blog_article_to_product', - 'blog_article_id', - 'blog_article', - 'id', - 'CASCADE', - 'CASCADE' - ); - - $this->addForeignKey( - 'blog_article_to_product_prod_fk', - 'blog_article_to_product', - 'product_id', - 'product', - 'id', - 'CASCADE', - 'CASCADE' - ); - } - - public function down() - { - $this->dropForeignKey('blog_article_to_product_prod_fk', 'blog_article_to_product'); - $this->dropForeignKey('blog_article_to_product_art_fk', 'blog_article_to_product'); - $this->dropIndex('blog_article_to_product_uk', 'blog_article_to_product'); - $this->dropTable('blog_article_to_product'); - } - } diff --git a/models/Article.php b/models/Article.php index ad115e0..a691832 100755 --- a/models/Article.php +++ b/models/Article.php @@ -3,7 +3,6 @@ namespace artbox\weblog\models; use artbox\core\behaviors\ManyToManyBehavior; - use artbox\catalog\models\Product; use artbox\core\models\Image; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; @@ -16,32 +15,31 @@ /** * 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 \artbox\catalog\models\Product $relatedProducts + * @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 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() @@ -66,6 +64,8 @@ public $articleIds = []; + public $productIds = []; + /** * @inheritdoc */ @@ -144,7 +144,7 @@ public function getRelatedProducts() { if (class_exists('\artbox\catalog\models\Product')) { - return $this->hasMany('\artbox\catalog\models\Product', [ 'id' => 'article_id' ]) + return $this->hasMany('\artbox\catalog\models\Product', [ 'id' => 'product_id' ]) ->via('articleToProduct'); } else { return ( new Query() )->where('1 = 0'); @@ -157,7 +157,7 @@ public function getArticleToProduct() { if (class_exists('\artbox\catalog\models\Product')) { - return $this->hasMany(ArticleToProduct::className(), [ 'id' => 'article_id' ]); + return $this->hasMany(ArticleToProduct::className(), [ 'article_id' => 'id' ]); } else { return ( new Query() )->where('1 = 0'); } @@ -201,15 +201,6 @@ /** * @return \yii\db\ActiveQuery */ - public function getProducts() - { - return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) - ->viaTable('blog_article_to_product', [ 'blog_article_id' => 'id' ]); - } - - /** - * @return \yii\db\ActiveQuery - */ public function getTags() { return $this->hasMany(Tag::className(), [ 'id' => 'blog_tag_id' ]) diff --git a/views/blog-article/_form.php b/views/blog-article/_form.php index b353b2a..8ef3b60 100755 --- a/views/blog-article/_form.php +++ b/views/blog-article/_form.php @@ -216,6 +216,63 @@ ); ?> + +
+ + 'productIds', + 'options' => [ + 'placeholder' => \Yii::t('blog', 'Search for products ...'), + 'multiple' => true, + ], + 'value' => array_keys($model->productIds), + 'data' => $model->productIds, + 'pluginOptions' => [ + 'allowClear' => true, + 'minimumInputLength' => 3, + 'language' => [ + 'errorLoading' => new JsExpression( + "function () { return 'Waiting for results...'; }" + ), + ], + 'ajax' => [ + 'url' => Url::to([ '/product/list' ]), + 'dataType' => 'json', + 'data' => new JsExpression( + 'function(params) { + return { + q:params.term + }; + }' + ), + ], + 'escapeMarkup' => new JsExpression( + 'function (markup) { + return markup; + }' + ), + 'templateResult' => new JsExpression( + 'function (product) { + return product.text; + }' + ), + 'templateSelection' => new JsExpression( + 'function (product) { + return product.text; + }' + ), + ], + ] + ); + ?> +
+ field($model, 'sort') ->textInput() ?> -- libgit2 0.21.4