Commit d7bd572580bc55ddd62ed43a113d21a940b62496
1 parent
adcaa25d
Blog article to product
Showing
4 changed files
with
115 additions
and
100 deletions
Show diff stats
controllers/ArticleController.php
@@ -155,13 +155,21 @@ | @@ -155,13 +155,21 @@ | ||
155 | 'lang.title' | 155 | 'lang.title' |
156 | ); | 156 | ); |
157 | 157 | ||
158 | + if (class_exists('\artbox\catalog\models\Product')) { | ||
159 | + $model->productIds = ArrayHelper::map( | ||
160 | + $model->relatedProducts, | ||
161 | + 'id', | ||
162 | + 'lang.title' | ||
163 | + ); | ||
164 | + } | ||
165 | + | ||
158 | if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { | 166 | if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { |
159 | $categories = Category::find() | 167 | $categories = Category::find() |
160 | ->where([ 'id' => \Yii::$app->request->post('categoryIds') ]) | 168 | ->where([ 'id' => \Yii::$app->request->post('categoryIds') ]) |
161 | ->all(); | 169 | ->all(); |
162 | - | 170 | + |
163 | $model->linkMany('categories', $categories); | 171 | $model->linkMany('categories', $categories); |
164 | - | 172 | + |
165 | $tags = Tag::find() | 173 | $tags = Tag::find() |
166 | ->where( | 174 | ->where( |
167 | [ | 175 | [ |
@@ -169,9 +177,28 @@ | @@ -169,9 +177,28 @@ | ||
169 | ] | 177 | ] |
170 | ) | 178 | ) |
171 | ->all(); | 179 | ->all(); |
172 | - | 180 | + |
173 | $model->linkMany('tags', $tags); | 181 | $model->linkMany('tags', $tags); |
182 | + | ||
183 | + if (class_exists('\artbox\catalog\models\Product')) { | ||
184 | + /** | ||
185 | + * @var \yii\db\ActiveQuery $query | ||
186 | + */ | ||
187 | + $query = call_user_func( | ||
188 | + [ | ||
189 | + '\artbox\catalog\models\Product', | ||
190 | + 'find', | ||
191 | + ] | ||
192 | + ); | ||
193 | + /** | ||
194 | + * @var \artbox\catalog\models\Product[] $products | ||
195 | + */ | ||
196 | + $products = $query->where([ 'id' => \Yii::$app->request->post('productIds') ]) | ||
197 | + ->all(); | ||
174 | 198 | ||
199 | + $model->linkMany('relatedProducts', $products); | ||
200 | + } | ||
201 | + | ||
175 | return $this->redirect( | 202 | return $this->redirect( |
176 | [ | 203 | [ |
177 | 'view', | 204 | 'view', |
migrations/m161101_144434_blog_article_to_product.php deleted
1 | -<?php | ||
2 | - | ||
3 | - use yii\db\Migration; | ||
4 | - | ||
5 | - class m161101_144434_blog_article_to_product extends Migration | ||
6 | - { | ||
7 | - public function up() | ||
8 | - { | ||
9 | - /** | ||
10 | - * Creates junction table and all stuff for adding related products to articles | ||
11 | - */ | ||
12 | - $this->createTable( | ||
13 | - 'blog_article_to_product', | ||
14 | - [ | ||
15 | - 'blog_article_id' => $this->integer() | ||
16 | - ->notNull(), | ||
17 | - 'product_id' => $this->integer() | ||
18 | - ->notNull(), | ||
19 | - ] | ||
20 | - ); | ||
21 | - | ||
22 | - $this->createIndex( | ||
23 | - 'blog_article_to_product_uk', | ||
24 | - 'blog_article_to_product', | ||
25 | - [ | ||
26 | - 'blog_article_id', | ||
27 | - 'product_id', | ||
28 | - ], | ||
29 | - true | ||
30 | - ); | ||
31 | - | ||
32 | - $this->addForeignKey( | ||
33 | - 'blog_article_to_product_art_fk', | ||
34 | - 'blog_article_to_product', | ||
35 | - 'blog_article_id', | ||
36 | - 'blog_article', | ||
37 | - 'id', | ||
38 | - 'CASCADE', | ||
39 | - 'CASCADE' | ||
40 | - ); | ||
41 | - | ||
42 | - $this->addForeignKey( | ||
43 | - 'blog_article_to_product_prod_fk', | ||
44 | - 'blog_article_to_product', | ||
45 | - 'product_id', | ||
46 | - 'product', | ||
47 | - 'id', | ||
48 | - 'CASCADE', | ||
49 | - 'CASCADE' | ||
50 | - ); | ||
51 | - } | ||
52 | - | ||
53 | - public function down() | ||
54 | - { | ||
55 | - $this->dropForeignKey('blog_article_to_product_prod_fk', 'blog_article_to_product'); | ||
56 | - $this->dropForeignKey('blog_article_to_product_art_fk', 'blog_article_to_product'); | ||
57 | - $this->dropIndex('blog_article_to_product_uk', 'blog_article_to_product'); | ||
58 | - $this->dropTable('blog_article_to_product'); | ||
59 | - } | ||
60 | - } |
models/Article.php
@@ -3,7 +3,6 @@ | @@ -3,7 +3,6 @@ | ||
3 | namespace artbox\weblog\models; | 3 | namespace artbox\weblog\models; |
4 | 4 | ||
5 | use artbox\core\behaviors\ManyToManyBehavior; | 5 | use artbox\core\behaviors\ManyToManyBehavior; |
6 | - use artbox\catalog\models\Product; | ||
7 | use artbox\core\models\Image; | 6 | use artbox\core\models\Image; |
8 | use yii\behaviors\TimestampBehavior; | 7 | use yii\behaviors\TimestampBehavior; |
9 | use yii\db\ActiveRecord; | 8 | use yii\db\ActiveRecord; |
@@ -16,32 +15,31 @@ | @@ -16,32 +15,31 @@ | ||
16 | /** | 15 | /** |
17 | * This is the model class for table "blog_article". | 16 | * This is the model class for table "blog_article". |
18 | * | 17 | * |
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 | 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 Tag[] $tags | ||
34 | + * @property \artbox\catalog\models\Product[] $relatedProducts | ||
37 | * * * From language behavior * | 35 | * * * From language behavior * |
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 | 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 | ||
45 | * @method string getOwnerKey() | 43 | * @method string getOwnerKey() |
46 | * @method void setOwnerKey( string $value ) | 44 | * @method void setOwnerKey( string $value ) |
47 | * @method string getLangKey() | 45 | * @method string getLangKey() |
@@ -66,6 +64,8 @@ | @@ -66,6 +64,8 @@ | ||
66 | 64 | ||
67 | public $articleIds = []; | 65 | public $articleIds = []; |
68 | 66 | ||
67 | + public $productIds = []; | ||
68 | + | ||
69 | /** | 69 | /** |
70 | * @inheritdoc | 70 | * @inheritdoc |
71 | */ | 71 | */ |
@@ -144,7 +144,7 @@ | @@ -144,7 +144,7 @@ | ||
144 | public function getRelatedProducts() | 144 | public function getRelatedProducts() |
145 | { | 145 | { |
146 | if (class_exists('\artbox\catalog\models\Product')) { | 146 | if (class_exists('\artbox\catalog\models\Product')) { |
147 | - return $this->hasMany('\artbox\catalog\models\Product', [ 'id' => 'article_id' ]) | 147 | + return $this->hasMany('\artbox\catalog\models\Product', [ 'id' => 'product_id' ]) |
148 | ->via('articleToProduct'); | 148 | ->via('articleToProduct'); |
149 | } else { | 149 | } else { |
150 | return ( new Query() )->where('1 = 0'); | 150 | return ( new Query() )->where('1 = 0'); |
@@ -157,7 +157,7 @@ | @@ -157,7 +157,7 @@ | ||
157 | public function getArticleToProduct() | 157 | public function getArticleToProduct() |
158 | { | 158 | { |
159 | if (class_exists('\artbox\catalog\models\Product')) { | 159 | if (class_exists('\artbox\catalog\models\Product')) { |
160 | - return $this->hasMany(ArticleToProduct::className(), [ 'id' => 'article_id' ]); | 160 | + return $this->hasMany(ArticleToProduct::className(), [ 'article_id' => 'id' ]); |
161 | } else { | 161 | } else { |
162 | return ( new Query() )->where('1 = 0'); | 162 | return ( new Query() )->where('1 = 0'); |
163 | } | 163 | } |
@@ -201,15 +201,6 @@ | @@ -201,15 +201,6 @@ | ||
201 | /** | 201 | /** |
202 | * @return \yii\db\ActiveQuery | 202 | * @return \yii\db\ActiveQuery |
203 | */ | 203 | */ |
204 | - public function getProducts() | ||
205 | - { | ||
206 | - return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) | ||
207 | - ->viaTable('blog_article_to_product', [ 'blog_article_id' => 'id' ]); | ||
208 | - } | ||
209 | - | ||
210 | - /** | ||
211 | - * @return \yii\db\ActiveQuery | ||
212 | - */ | ||
213 | public function getTags() | 204 | public function getTags() |
214 | { | 205 | { |
215 | return $this->hasMany(Tag::className(), [ 'id' => 'blog_tag_id' ]) | 206 | return $this->hasMany(Tag::className(), [ 'id' => 'blog_tag_id' ]) |
views/blog-article/_form.php
@@ -216,6 +216,63 @@ | @@ -216,6 +216,63 @@ | ||
216 | ); | 216 | ); |
217 | ?> | 217 | ?> |
218 | </div> | 218 | </div> |
219 | + <?php | ||
220 | + if (class_exists('\artbox\catalog\models\Product')) { | ||
221 | + ?> | ||
222 | + <div class="form-group"> | ||
223 | + <label class="control-label"><?= \Yii::t('blog', 'Products'); ?></label> | ||
224 | + <?php | ||
225 | + echo Select2::widget( | ||
226 | + [ | ||
227 | + 'name' => 'productIds', | ||
228 | + 'options' => [ | ||
229 | + 'placeholder' => \Yii::t('blog', 'Search for products ...'), | ||
230 | + 'multiple' => true, | ||
231 | + ], | ||
232 | + 'value' => array_keys($model->productIds), | ||
233 | + 'data' => $model->productIds, | ||
234 | + 'pluginOptions' => [ | ||
235 | + 'allowClear' => true, | ||
236 | + 'minimumInputLength' => 3, | ||
237 | + 'language' => [ | ||
238 | + 'errorLoading' => new JsExpression( | ||
239 | + "function () { return 'Waiting for results...'; }" | ||
240 | + ), | ||
241 | + ], | ||
242 | + 'ajax' => [ | ||
243 | + 'url' => Url::to([ '/product/list' ]), | ||
244 | + 'dataType' => 'json', | ||
245 | + 'data' => new JsExpression( | ||
246 | + 'function(params) { | ||
247 | + return { | ||
248 | + q:params.term | ||
249 | + }; | ||
250 | + }' | ||
251 | + ), | ||
252 | + ], | ||
253 | + 'escapeMarkup' => new JsExpression( | ||
254 | + 'function (markup) { | ||
255 | + return markup; | ||
256 | + }' | ||
257 | + ), | ||
258 | + 'templateResult' => new JsExpression( | ||
259 | + 'function (product) { | ||
260 | + return product.text; | ||
261 | + }' | ||
262 | + ), | ||
263 | + 'templateSelection' => new JsExpression( | ||
264 | + 'function (product) { | ||
265 | + return product.text; | ||
266 | + }' | ||
267 | + ), | ||
268 | + ], | ||
269 | + ] | ||
270 | + ); | ||
271 | + ?> | ||
272 | + </div> | ||
273 | + <?php | ||
274 | + } | ||
275 | + ?> | ||
219 | 276 | ||
220 | <?= $form->field($model, 'sort') | 277 | <?= $form->field($model, 'sort') |
221 | ->textInput() ?> | 278 | ->textInput() ?> |