diff --git a/models/Category.php b/models/Category.php index 0057f7c..8458788 100755 --- a/models/Category.php +++ b/models/Category.php @@ -29,6 +29,7 @@ * @property ProductCategory[] $productCategories * @property Brand[] $brands * @property TaxGroup[] $taxGroups + * @property Category[] $siblings * * From language behavior * * @property CategoryLang $lang * @property CategoryLang[] $langs @@ -149,7 +150,7 @@ public function getProducts() { return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) - ->viaTable('product_category', [ 'category_id' => 'id' ])->inverseOf('categories'); + ->viaTable('product_category', [ 'category_id' => 'id' ]); } /** @@ -309,4 +310,20 @@ return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ]) ->viaTable('tax_group_to_category', [ 'category_id' => 'id' ]); } + + /** + * Get query to obtain siblings of current category (categories with same parent) + * + * @return ActiveQuery + */ + public function getSiblings() + { + return $this->hasMany($this::className(), [ 'parent_id' => 'parent_id' ]) + ->andWhere( + [ + 'not', + [ 'id' => $this->id ], + ] + ); + } } diff --git a/models/Product.php b/models/Product.php index 8096f49..3e7ddd1 100755 --- a/models/Product.php +++ b/models/Product.php @@ -365,7 +365,7 @@ public function getCategories() { return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) - ->viaTable('product_category', [ 'product_id' => 'id' ])->inverseOf('products'); + ->viaTable('product_category', [ 'product_id' => 'id' ]); } /** -- libgit2 0.21.4