From f540c70bc40cd9cec9378dc6e229631d51d7272b Mon Sep 17 00:00:00 2001 From: yarik Date: Mon, 14 Nov 2016 19:22:25 +0200 Subject: [PATCH] Category finish --- models/Category.php | 19 ++++++++++++++++++- models/Product.php | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) 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