Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tree][NestedSet] Fix TreeRoot without rootIdentifierMethod when calling getNextSiblings #2518

Merged
merged 4 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Tree/Entity/Repository/NestedTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,11 @@ public function getNextSiblingsQueryBuilder($node, $includeSelf = false)
} elseif (isset($config['root'])) {
$qb->andWhere($qb->expr()->eq('node.'.$config['root'], ':root'));
$qb->andWhere($qb->expr()->isNull('node.'.$config['parent']));
$method = $config['rootIdentifierMethod'];
$qb->setParameter('root', $node->$method());
$root = isset($config['rootIdentifierMethod']) ?
$node->{$config['rootIdentifierMethod']}() :
$wrapped->getPropertyValue($config['root'])
;
$qb->setParameter('root', $root);
} else {
$qb->andWhere($qb->expr()->isNull('node.'.$config['parent']));
}
Expand Down
143 changes: 143 additions & 0 deletions tests/Gedmo/Tree/Fixture/Issue2517/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Tree\Fixture\Issue2517;

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;

/**
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="categories")
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
#[Gedmo\Tree(type: 'nested')]
#[ORM\Table(name: 'categories')]
#[ORM\Entity(repositoryClass: NestedTreeRepository::class)]
class Category
{
/**
* @var int|null
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private $id;

/**
* @var string|null
*
* @ORM\Column(name="title", type="string", length=64)
*/
#[ORM\Column(name: 'title', type: Types::STRING, length: 64)]
private $title;

/**
* @var int|null
*
* @Gedmo\TreeLeft
* @ORM\Column(name="lft", type="integer")
*/
#[ORM\Column(name: 'lft', type: Types::INTEGER)]
#[Gedmo\TreeLeft]
private $lft;

/**
* @var int|null
*
* @Gedmo\TreeRight
* @ORM\Column(name="rgt", type="integer")
*/
#[ORM\Column(name: 'rgt', type: Types::INTEGER)]
#[Gedmo\TreeRight]
private $rgt;

/**
* @var int|null
*
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
#[ORM\Column(name: 'lvl', type: Types::INTEGER)]
#[Gedmo\TreeLevel]
private $lvl;

/**
* @var self|null
*
* @Gedmo\TreeRoot
* @ORM\ManyToOne(targetEntity="Category")
* @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
*/
#[Gedmo\TreeRoot]
#[ORM\ManyToOne(targetEntity: self::class)]
#[ORM\JoinColumn(name: 'tree_root', referencedColumnName: 'id', onDelete: 'CASCADE')]
private $root;

/**
* @var self|null
*
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
#[Gedmo\TreeParent]
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
private $parent;

/**
* @var Collection<int, Category>
*
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
* @ORM\OrderBy({"lft" = "ASC"})
*/
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
#[ORM\OrderBy(['lft' => 'ASC'])]
private $children;

public function getId(): ?int
{
return $this->id;
}

public function setTitle(?string $title): void
{
$this->title = $title;
}

public function getTitle(): ?string
{
return $this->title;
}

public function getRoot(): ?self
{
return $this->root;
}

public function setParent(self $parent = null): void
Jean-Beru marked this conversation as resolved.
Show resolved Hide resolved
{
$this->parent = $parent;
}

public function getParent(): ?self
{
return $this->parent;
}
}
68 changes: 68 additions & 0 deletions tests/Gedmo/Tree/Issue/Issue2517Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Tree\Issue;

use Doctrine\Common\EventManager;
use Gedmo\Tests\Tool\BaseTestCaseORM;
use Gedmo\Tests\Tree\Fixture\Issue2517\Category;
use Gedmo\Tree\TreeListener;

final class Issue2517Test extends BaseTestCaseORM
{
/**
* @var TreeListener
*/
private $listener;

protected function setUp(): void
{
parent::setUp();

$this->listener = new TreeListener();

$evm = new EventManager();
$evm->addEventSubscriber($this->listener);

$this->getDefaultMockSqliteEntityManager($evm);
}

public function testGetNextSiblingsWithoutIdentifierMethod(): void
{
$food = new Category();
$food->setTitle('Food');

$fruits = new Category();
$fruits->setTitle('Fruits');
$fruits->setParent($food);

$vegetables = new Category();
$vegetables->setTitle('Vegetables');
$vegetables->setParent($food);

$this->em->persist($food);
$this->em->persist($fruits);
$this->em->persist($vegetables);
$this->em->flush();

$categoryRepository = $this->em->getRepository(Category::class);

static::assertTrue($categoryRepository->verify());
static::assertCount(0, $categoryRepository->getNextSiblings($food));
static::assertCount(1, $categoryRepository->getNextSiblings($fruits));
static::assertCount(0, $categoryRepository->getNextSiblings($vegetables));
}

protected function getUsedEntityFixtures(): array
{
return [Category::class];
}
}