-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Tree][NestedSet] Fix inserting multiple roots in one flush #2932
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
Open
sgehrig
wants to merge
9
commits into
doctrine-extensions:main
Choose a base branch
from
sgehrig:bug/#2582-multiple-roots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
883f84d
adds testcase to reproduce https://github.com/doctrine-extensions/Doc…
sgehrig 5abc6a4
this fixes the issue although I am not 100% sure why
sgehrig 43cfbe9
fixes PHP 7.4 compatibility
sgehrig f2e8978
fixes double ) in annotation
sgehrig cd126db
fixes index for doctrine orm annotations
sgehrig d9d04c0
fixes PHP-CS and PHPStan reportings
sgehrig cf528c8
fixes sone more PHP-CS reportings
sgehrig d29bde7
made test class final as per suggestion
sgehrig ce527f3
[#2582] added required changelog entry
sgehrig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <[email protected]> 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\Issue2582; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Gedmo\Mapping\Annotation as Gedmo; | ||
|
||
/** | ||
* @Gedmo\Tree(type="nested") | ||
* | ||
* @ORM\Table( | ||
* name="ous", | ||
* indexes={ | ||
* @ORM\Index(name="idx_tree", fields={"left", "right"}) | ||
* } | ||
* ) | ||
* @ORM\Entity() | ||
*/ | ||
#[ORM\Table(name: 'ous')] | ||
#[ORM\Index(name: 'idx_tree', columns: ['lft', 'rgt'])] | ||
#[ORM\Entity] | ||
#[Gedmo\Tree(type: 'nested')] | ||
class OU | ||
{ | ||
/** | ||
* @ORM\Column(name="id", type="guid") | ||
* @ORM\Id() | ||
*/ | ||
#[ORM\Column('id', 'guid')] | ||
#[ORM\Id] | ||
private string $id; | ||
|
||
/** | ||
* @Gedmo\TreeParent() | ||
* | ||
* @ORM\ManyToOne(targetEntity="\Gedmo\Tests\Tree\Fixture\Issue2582\OU", inversedBy="children") | ||
* @ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true, onDelete="CASCADE") | ||
*/ | ||
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] | ||
#[ORM\JoinColumn(name: 'parent', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] | ||
#[Gedmo\TreeParent] | ||
private ?self $parent = null; | ||
|
||
/** | ||
* @Gedmo\TreeLeft() | ||
* | ||
* @ORM\Column(name="lft", type="integer", options={"unsigned"=true}) | ||
*/ | ||
#[ORM\Column(name: 'lft', type: 'integer', options: ['unsigned' => true])] | ||
#[Gedmo\TreeLeft] | ||
private int $left = 1; | ||
|
||
/** | ||
* @Gedmo\TreeLevel() | ||
* | ||
* @ORM\Column(name="lvl", type="integer", options={"unsigned"=true}) | ||
*/ | ||
#[ORM\Column(name: 'lvl', type: 'integer', options: ['unsigned' => true])] | ||
#[Gedmo\TreeLevel] | ||
private int $level = 0; | ||
|
||
/** | ||
* @Gedmo\TreeRight() | ||
* | ||
* @ORM\Column(name="rgt", type="integer", options={"unsigned"=true}) | ||
*/ | ||
#[ORM\Column(name: 'rgt', type: 'integer', options: ['unsigned' => true])] | ||
#[Gedmo\TreeRight] | ||
private int $right = 2; | ||
|
||
/** | ||
* @ORM\OneToMany(targetEntity="\Gedmo\Tests\Tree\Fixture\Issue2582\OU", mappedBy="parent") | ||
* @ORM\OrderBy({"left" = "ASC"}) | ||
* | ||
* @var Collection<int, self> | ||
*/ | ||
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')] | ||
#[ORM\OrderBy(['left' => 'ASC'])] | ||
private Collection $children; | ||
|
||
public function __construct(string $id, ?self $parent = null) | ||
{ | ||
$this->id = $id; | ||
$this->children = new ArrayCollection(); | ||
$this->parent = $parent; | ||
if ($parent) { | ||
$parent->children->add($this); | ||
} | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getParent(): ?self | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
public function getLeft(): int | ||
{ | ||
return $this->left; | ||
} | ||
|
||
public function getLevel(): int | ||
{ | ||
return $this->level; | ||
} | ||
|
||
public function getRight(): int | ||
{ | ||
return $this->right; | ||
} | ||
|
||
/** | ||
* @return Collection<int, self> | ||
*/ | ||
public function getChildren(): Collection | ||
{ | ||
return $this->children; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <[email protected]> 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\Issue2582\OU; | ||
use Gedmo\Tree\TreeListener; | ||
|
||
class Issue2582Test extends BaseTestCaseORM | ||
{ | ||
private TreeListener $listener; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->listener = new TreeListener(); | ||
|
||
$evm = new EventManager(); | ||
$evm->addEventSubscriber($this->listener); | ||
|
||
$this->getDefaultMockSqliteEntityManager($evm); | ||
} | ||
|
||
public function testInsertTwoRootsInOneFlush(): void | ||
{ | ||
$ou1 = new OU('00000000-0000-0000-0000-000000000001', null); | ||
$ou11 = new OU('00000000-0000-0000-0000-000000000011', $ou1); | ||
$ou2 = new OU('00000000-0000-0000-0000-000000000002', null); | ||
$ou21 = new OU('00000000-0000-0000-0000-000000000021', $ou2); | ||
|
||
$this->em->persist($ou1); | ||
$this->em->persist($ou11); | ||
$this->em->persist($ou2); | ||
$this->em->persist($ou21); | ||
$this->em->flush(); | ||
|
||
$this->em->clear(); | ||
|
||
$expected = [ | ||
['00000000-0000-0000-0000-000000000001', null, 1, 0, 4], | ||
['00000000-0000-0000-0000-000000000011', '00000000-0000-0000-0000-000000000001', 2, 1, 3], | ||
['00000000-0000-0000-0000-000000000002', null, 5, 0, 8], | ||
['00000000-0000-0000-0000-000000000021', '00000000-0000-0000-0000-000000000002', 6, 1, 7], | ||
]; | ||
foreach ($this->fetchAllOUs() as $i => $a) { | ||
static::assertSame( | ||
$expected[$i], | ||
[ | ||
$a->getId(), | ||
$a->getParent() ? $a->getParent()->getId() : null, | ||
$a->getLeft(), | ||
$a->getLevel(), | ||
$a->getRight(), | ||
], | ||
); | ||
} | ||
} | ||
|
||
public function testInsertTwoRootsInOneFlushRootsFirst(): void | ||
{ | ||
$ou1 = new OU('00000000-0000-0000-0000-000000000001', null); | ||
$ou11 = new OU('00000000-0000-0000-0000-000000000011', $ou1); | ||
$ou2 = new OU('00000000-0000-0000-0000-000000000002', null); | ||
$ou21 = new OU('00000000-0000-0000-0000-000000000021', $ou2); | ||
|
||
$this->em->persist($ou1); | ||
$this->em->persist($ou2); | ||
$this->em->persist($ou11); | ||
$this->em->persist($ou21); | ||
$this->em->flush(); | ||
|
||
$this->em->clear(); | ||
|
||
$expected = [ | ||
['00000000-0000-0000-0000-000000000001', null, 1, 0, 4], | ||
['00000000-0000-0000-0000-000000000011', '00000000-0000-0000-0000-000000000001', 2, 1, 3], | ||
['00000000-0000-0000-0000-000000000002', null, 5, 0, 8], | ||
['00000000-0000-0000-0000-000000000021', '00000000-0000-0000-0000-000000000002', 6, 1, 7], | ||
]; | ||
foreach ($this->fetchAllOUs() as $i => $a) { | ||
static::assertSame( | ||
$expected[$i], | ||
[ | ||
$a->getId(), | ||
$a->getParent() ? $a->getParent()->getId() : null, | ||
$a->getLeft(), | ||
$a->getLevel(), | ||
$a->getRight(), | ||
], | ||
); | ||
} | ||
} | ||
|
||
public function testInsertTwoRootsInTwoFlushes(): void | ||
{ | ||
$ou1 = new OU('00000000-0000-0000-0000-000000000001', null); | ||
$ou11 = new OU('00000000-0000-0000-0000-000000000011', $ou1); | ||
$ou2 = new OU('00000000-0000-0000-0000-000000000002', null); | ||
$ou21 = new OU('00000000-0000-0000-0000-000000000021', $ou2); | ||
|
||
$this->em->persist($ou1); | ||
$this->em->persist($ou11); | ||
$this->em->flush(); | ||
$this->em->persist($ou2); | ||
$this->em->persist($ou21); | ||
$this->em->flush(); | ||
|
||
$this->em->clear(); | ||
|
||
$expected = [ | ||
['00000000-0000-0000-0000-000000000001', null, 1, 0, 4], | ||
['00000000-0000-0000-0000-000000000011', '00000000-0000-0000-0000-000000000001', 2, 1, 3], | ||
['00000000-0000-0000-0000-000000000002', null, 5, 0, 8], | ||
['00000000-0000-0000-0000-000000000021', '00000000-0000-0000-0000-000000000002', 6, 1, 7], | ||
]; | ||
foreach ($this->fetchAllOUs() as $i => $a) { | ||
static::assertSame( | ||
$expected[$i], | ||
[ | ||
$a->getId(), | ||
$a->getParent() ? $a->getParent()->getId() : null, | ||
$a->getLeft(), | ||
$a->getLevel(), | ||
$a->getRight(), | ||
], | ||
); | ||
} | ||
} | ||
|
||
protected function getUsedEntityFixtures(): array | ||
{ | ||
return [OU::class]; | ||
} | ||
|
||
/** | ||
* @return list<OU> | ||
*/ | ||
private function fetchAllOUs(): array | ||
{ | ||
$categoryRepo = $this->em->getRepository(OU::class); | ||
|
||
return $categoryRepo | ||
->createQueryBuilder('ou') | ||
->orderBy('ou.left', 'ASC') | ||
->getQuery() | ||
->getResult(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.