fixed getRootCategory

This commit is contained in:
peternuernberger 2020-01-06 16:52:48 +01:00
parent 6f4b7474cb
commit 90a182ec94
2 changed files with 11 additions and 11 deletions

View File

@ -55,14 +55,15 @@ class Category extends TranslatableModel implements CategoryContract
} }
/** /**
* Getting the root category of a category
* *
* @return Category
*/ */
public function rootCategory(): Category public function getRootCategory(): Category
{ {
return $this->hasOne(Category::modelClass()) return Category::whereNull('parent_id')
->where('parent_id', null) ->where('_lft', '<=', $this->_lft)
->andWhere('_lft', '<=', $this->_lft) ->where('_rgt', '>=', $this->_rgt)
->andWhere('_rgt', '>=', $this->_rgt)
->first(); ->first();
} }
@ -95,8 +96,7 @@ class Category extends TranslatableModel implements CategoryContract
private function findInTree($categoryTree = null): Category private function findInTree($categoryTree = null): Category
{ {
if (! $categoryTree) { if (! $categoryTree) {
$rootCategoryId = core()->getCurrentChannel()->rootCategory->id; $categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($this->getRootCategory()->id);
$categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($rootCategoryId);
} }
foreach ($categoryTree as $category) { foreach ($categoryTree as $category) {

View File

@ -41,12 +41,12 @@ class CategoryCest
'slug' => 'root', 'slug' => 'root',
'locale' => 'en', 'locale' => 'en',
]); ]);
$rootCategory = $I->grabRecord(Category::class, [ $this->rootCategory = $I->grabRecord(Category::class, [
'id' => $rootCategoryTranslation->category_id, 'id' => $rootCategoryTranslation->category_id,
]); ]);
$this->categoryAttributes = [ $this->categoryAttributes = [
'parent_id' => $rootCategory->id, 'parent_id' => $this->rootCategory->id,
'position' => 0, 'position' => 0,
'status' => 1, 'status' => 1,
$this->localeEn->code => [ $this->localeEn->code => [
@ -58,7 +58,7 @@ class CategoryCest
]; ];
$this->category = $I->make(Category::class, $this->categoryAttributes)->first(); $this->category = $I->make(Category::class, $this->categoryAttributes)->first();
$rootCategory->prependNode($this->category); $this->rootCategory->prependNode($this->category);
$I->assertNotNull($this->category); $I->assertNotNull($this->category);
$I->seeRecord(CategoryTranslation::class, [ $I->seeRecord(CategoryTranslation::class, [
@ -159,7 +159,7 @@ class CategoryCest
public function testGetRootCategory(UnitTester $I) public function testGetRootCategory(UnitTester $I)
{ {
$I->wantTo('test rootCategory attribute of a category'); $I->wantTo('test rootCategory attribute of a category');
$rootCategory = $this->grandChildCategory->rootCategory; $rootCategory = $this->grandChildCategory->getRootCategory();
$I->assertNotNull($rootCategory); $I->assertNotNull($rootCategory);
$I->assertEquals($rootCategory->id, $this->rootCategory->id); $I->assertEquals($rootCategory->id, $this->rootCategory->id);