Commit f540c70bc40cd9cec9378dc6e229631d51d7272b
1 parent
72a1aa2d
Category finish
Showing
2 changed files
with
19 additions
and
2 deletions
Show diff stats
models/Category.php
| ... | ... | @@ -29,6 +29,7 @@ |
| 29 | 29 | * @property ProductCategory[] $productCategories |
| 30 | 30 | * @property Brand[] $brands |
| 31 | 31 | * @property TaxGroup[] $taxGroups |
| 32 | + * @property Category[] $siblings | |
| 32 | 33 | * * From language behavior * |
| 33 | 34 | * @property CategoryLang $lang |
| 34 | 35 | * @property CategoryLang[] $langs |
| ... | ... | @@ -149,7 +150,7 @@ |
| 149 | 150 | public function getProducts() |
| 150 | 151 | { |
| 151 | 152 | return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) |
| 152 | - ->viaTable('product_category', [ 'category_id' => 'id' ])->inverseOf('categories'); | |
| 153 | + ->viaTable('product_category', [ 'category_id' => 'id' ]); | |
| 153 | 154 | } |
| 154 | 155 | |
| 155 | 156 | /** |
| ... | ... | @@ -309,4 +310,20 @@ |
| 309 | 310 | return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ]) |
| 310 | 311 | ->viaTable('tax_group_to_category', [ 'category_id' => 'id' ]); |
| 311 | 312 | } |
| 313 | + | |
| 314 | + /** | |
| 315 | + * Get query to obtain siblings of current category (categories with same parent) | |
| 316 | + * | |
| 317 | + * @return ActiveQuery | |
| 318 | + */ | |
| 319 | + public function getSiblings() | |
| 320 | + { | |
| 321 | + return $this->hasMany($this::className(), [ 'parent_id' => 'parent_id' ]) | |
| 322 | + ->andWhere( | |
| 323 | + [ | |
| 324 | + 'not', | |
| 325 | + [ 'id' => $this->id ], | |
| 326 | + ] | |
| 327 | + ); | |
| 328 | + } | |
| 312 | 329 | } | ... | ... |
models/Product.php
| ... | ... | @@ -365,7 +365,7 @@ |
| 365 | 365 | public function getCategories() |
| 366 | 366 | { |
| 367 | 367 | return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) |
| 368 | - ->viaTable('product_category', [ 'product_id' => 'id' ])->inverseOf('products'); | |
| 368 | + ->viaTable('product_category', [ 'product_id' => 'id' ]); | |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | /** | ... | ... |