-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Allow multiple entities with closure strategy #2653
Open
JDruery
wants to merge
20
commits into
doctrine-extensions:main
Choose a base branch
from
JDruery:main
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 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
436a6e8
Attempt to fix issue with multiple entities
JDruery 30bbe08
Issue #2652 fix
JDruery a387a46
Revert versions to original
JDruery bc9d5f6
Conform to coding standards
JDruery 24d4835
Recommended updates after review by maintainer
JDruery b414efb
Remove unneeded code
JDruery 83243d4
Make comment more clear
JDruery e394706
Revert to original file
JDruery f84b6e3
Clarify reason for unset of pendingChildNodeInserts
JDruery 7fc5196
Add changelog note for #2653
JDruery 0cd1141
Ensure the node is persisted before inserting closure
JDruery c83d259
Use null === instead is_null
JDruery 65ebca2
revert to upstream
JDruery c338cc6
Process node types one at a time in setLevelFieldOnPendingNodes
JDruery 82fd6b2
Missing $className variable
JDruery 2be48b9
Correct typo in comment
JDruery 29fd9bf
Modify test for code coverage
JDruery eac3291
Comply with coding standards
JDruery ce419ec
Match php version of upstream
JDruery 484aa78
Match packages of upstream
JDruery 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 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 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 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 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,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\Issue2652; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
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\ClosureTreeRepository; | ||
|
||
/** | ||
* @Gedmo\Tree(type="closure") | ||
* @Gedmo\TreeClosure(class="Category2Closure") | ||
* | ||
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\ClosureTreeRepository") | ||
*/ | ||
#[ORM\Entity(repositoryClass: ClosureTreeRepository::class)] | ||
#[Gedmo\Tree(type: 'closure')] | ||
#[Gedmo\TreeClosure(class: Category2Closure::class)] | ||
class Category2 | ||
{ | ||
/** | ||
* @var int|null | ||
* | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column(type: Types::INTEGER)] | ||
private $id; | ||
|
||
/** | ||
* @ORM\Column(name="title", type="string", length=64) | ||
*/ | ||
#[ORM\Column(name: 'title', type: Types::STRING, length: 64)] | ||
private ?string $title = null; | ||
|
||
/** | ||
* @ORM\Column(name="level", type="integer", nullable=true) | ||
* | ||
* @Gedmo\TreeLevel | ||
*/ | ||
#[ORM\Column(name: 'level', type: Types::INTEGER, nullable: true)] | ||
#[Gedmo\TreeLevel] | ||
private ?int $level = null; | ||
|
||
/** | ||
* @Gedmo\TreeParent | ||
* | ||
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE") | ||
* @ORM\ManyToOne(targetEntity="Category2", inversedBy="children") | ||
*/ | ||
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] | ||
#[ORM\JoinColumn(name: 'category2_id', referencedColumnName: 'id', onDelete: 'CASCADE')] | ||
#[Gedmo\TreeParent] | ||
private ?\Gedmo\Tests\Tree\Fixture\Issue2652\Category2 $parent = null; | ||
|
||
/** | ||
* @var Collection<int, Category2Closure> | ||
*/ | ||
private $closures; | ||
|
||
public function __construct() | ||
{ | ||
$this->closures = new ArrayCollection(); | ||
} | ||
|
||
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 setParent(?self $parent = null): void | ||
{ | ||
$this->parent = $parent; | ||
} | ||
|
||
public function getParent(): ?self | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
public function addClosure(Category2Closure $closure): void | ||
{ | ||
$this->closures[] = $closure; | ||
} | ||
|
||
public function setLevel(?int $level): void | ||
{ | ||
$this->level = $level; | ||
} | ||
|
||
public function getLevel(): ?int | ||
{ | ||
return $this->level; | ||
} | ||
} |
This file contains 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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\Issue2652; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Gedmo\Tree\Entity\MappedSuperclass\AbstractClosure; | ||
|
||
/** | ||
* @ORM\Entity | ||
* @ORM\Table( | ||
* indexes={@ORM\Index(name="closure_category2_depth_idx", columns={"depth"})}, | ||
* uniqueConstraints={@ORM\UniqueConstraint(name="closure_category2_unique_idx", columns={ | ||
* "ancestor", "descendant" | ||
* })} | ||
* ) | ||
*/ | ||
#[ORM\Entity] | ||
#[ORM\UniqueConstraint(name: 'closure_category2_unique_idx', columns: ['ancestor', 'descendant'])] | ||
#[ORM\Index(name: 'closure_category2_depth_idx', columns: ['depth'])] | ||
class Category2Closure extends AbstractClosure | ||
{ | ||
/** | ||
* @ORM\ManyToOne(targetEntity="Gedmo\Tests\Tree\Fixture\Issue2652\Category2") | ||
* @ORM\JoinColumn(name="ancestor", referencedColumnName="id", nullable=false, onDelete="CASCADE") | ||
*/ | ||
#[ORM\ManyToOne(targetEntity: Category2::class)] | ||
#[ORM\JoinColumn(name: 'ancestor', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] | ||
protected $ancestor; | ||
|
||
/** | ||
* @ORM\ManyToOne(targetEntity="Gedmo\Tests\Tree\Fixture\Issue2652\Category2") | ||
* @ORM\JoinColumn(name="descendant", referencedColumnName="id", nullable=false, onDelete="CASCADE") | ||
*/ | ||
#[ORM\ManyToOne(targetEntity: Category2::class)] | ||
#[ORM\JoinColumn(name: 'descendant', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] | ||
protected $descendant; | ||
} |
This file contains 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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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; | ||
|
||
use Doctrine\Common\EventManager; | ||
use Gedmo\Tests\Tool\BaseTestCaseORM; | ||
use Gedmo\Tests\Tree\Fixture\Closure\Category; | ||
use Gedmo\Tests\Tree\Fixture\Closure\CategoryClosure; | ||
use Gedmo\Tests\Tree\Fixture\Issue2652\Category2; | ||
use Gedmo\Tests\Tree\Fixture\Issue2652\Category2Closure; | ||
use Gedmo\Tree\TreeListener; | ||
|
||
/** | ||
* These are tests for Tree behavior | ||
* | ||
* @author Gustavo Adrian <[email protected]> | ||
* @author Gediminas Morkevicius <[email protected]> | ||
*/ | ||
final class Issue2652Test extends BaseTestCaseORM | ||
{ | ||
private const CATEGORY = Category::class; | ||
private const CLOSURE = CategoryClosure::class; | ||
private const CATEGORY2 = Category2::class; | ||
private const CLOSURE2 = Category2Closure::class; | ||
|
||
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 testAddMultipleEntityTypes(): void | ||
{ | ||
$food = new Category(); | ||
$food->setTitle('Food'); | ||
$this->em->persist($food); | ||
|
||
$monkey = new Category2(); | ||
$monkey->setTitle('Monkey'); | ||
$this->em->persist($monkey); | ||
|
||
// Before issue #2652 was fixed, null values would be inserted into the closure table at this next line and an exception would be thrown | ||
$this->em->flush(); | ||
|
||
// Ensure that the closures were correctly inserted | ||
$repo = $this->em->getRepository(self::CATEGORY); | ||
|
||
$food = $repo->findOneBy(['title' => 'Food']); | ||
$dql = 'SELECT c FROM '.self::CLOSURE.' c'; | ||
$dql .= ' WHERE c.ancestor = :ancestor'; | ||
$query = $this->em->createQuery($dql); | ||
$query->setParameter('ancestor', $food); | ||
|
||
$foodClosures = $query->getResult(); | ||
static::assertCount(1, $foodClosures); | ||
|
||
$repo = $this->em->getRepository(self::CATEGORY2); | ||
$monkey = $repo->findOneBy(['title' => 'Monkey']); | ||
$dql = 'SELECT c FROM '.self::CLOSURE2.' c'; | ||
$dql .= ' WHERE c.ancestor = :ancestor'; | ||
$query = $this->em->createQuery($dql); | ||
$query->setParameter('ancestor', $monkey); | ||
|
||
$monkeyClosures = $query->getResult(); | ||
static::assertCount(1, $monkeyClosures); | ||
} | ||
|
||
protected function getUsedEntityFixtures(): array | ||
{ | ||
return [ | ||
self::CATEGORY, | ||
self::CLOSURE, | ||
self::CATEGORY2, | ||
self::CLOSURE2, | ||
]; | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, check the whole file. Many of these changes seem like a regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your patience, this is my first time contributing to a large project. I've gone through the whole file and updated all packages to match the current versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to iterate and fix, JDruery!