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())
->where('parent_id', null)
->andWhere('_lft', '<=', $this->_lft)
->andWhere('_rgt', '>=', $this->_rgt)
return Category::whereNull('parent_id')
->where('_lft', '<=', $this->_lft)
->where('_rgt', '>=', $this->_rgt)
->first();
}
@ -95,8 +96,7 @@ class Category extends TranslatableModel implements CategoryContract
private function findInTree($categoryTree = null): Category
{
if (! $categoryTree) {
$rootCategoryId = core()->getCurrentChannel()->rootCategory->id;
$categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($rootCategoryId);
$categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($this->getRootCategory()->id);
}
foreach ($categoryTree as $category) {

View File

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