Skip to content

Commit

Permalink
Reproduce exception on same class relation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonberger committed Jan 2, 2023
1 parent 85ac276 commit 2abff4c
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/Doctrine/Tests/Models/GH10348/GH10348ChildEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Doctrine\Tests\Models\GH10348;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="gh10348_child_entities")
*/
#[ORM\Entity]
#[ORM\Table(name: 'gh10348_child_entities')]
class GH10348ChildEntity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private ?int $id = null;

/**
* @ORM\ManyToOne(targetEntity="GH10348ParentEntity", inversedBy="children")
*/
#[ORM\ManyToOne(targetEntity: GH10348ParentEntity::class, inversedBy: 'children')]
private ?GH10348ParentEntity $parent = null;

/**
* @ORM\ManyToOne(targetEntity="GH10348ChildEntity", cascade={"remove"})
*/
#[ORM\ManyToOne(targetEntity: GH10348ChildEntity::class, cascade: ['remove'])]
private ?GH10348ChildEntity $origin = null;

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

public function setId(?int $id): void
{
$this->id = $id;
}

public function getParent(): ?GH10348ParentEntity
{
return $this->parent;
}

public function setParent(?GH10348ParentEntity $parent): void
{
$this->parent = $parent;
}

public function getOrigin(): ?GH10348ChildEntity
{
return $this->origin;
}

public function setOrigin(?GH10348ChildEntity $origin): void
{
$this->origin = $origin;
}
}
50 changes: 50 additions & 0 deletions tests/Doctrine/Tests/Models/GH10348/GH10348ParentEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Doctrine\Tests\Models\GH10348;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="gh10348_parent_entities")
*/
#[ORM\Entity]
#[ORM\Table(name: 'gh10348_parent_entities')]
class GH10348ParentEntity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private ?int $id = null;

/**
* @ORM\OneToMany(targetEntity="GH10348ChildEntity", mappedBy="parent", cascade={"persist", "remove"})
*/
#[ORM\OneToMany(targetEntity: GH10348ChildEntity::class, mappedBy: 'parent', cascade: ['persist', 'remove'])]
private Collection $children;

public function __construct()
{
$this->children = new ArrayCollection();
}

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

public function addChild(GH10348ChildEntity $childEntity): self
{
$childEntity->setParent($this);
$this->children->add($childEntity);

return $this;
}
}
43 changes: 43 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH10348Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Models\GH10348\GH10348ChildEntity;
use Doctrine\Tests\Models\GH10348\GH10348ParentEntity;
use Doctrine\Tests\OrmFunctionalTestCase;

final class GH10348Test extends OrmFunctionalTestCase
{
public function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
GH10348ChildEntity::class,
GH10348ParentEntity::class,
]);
}

public function testCanRemoveParentWithChildRelatesToOwnEntity(): void
{
$child1 = new GH10348ChildEntity();
$child2 = new GH10348ChildEntity();
$child2->setOrigin($child1);

$parent = new GH10348ParentEntity();
$parent->addChild($child1)->addChild($child2);

$this->_em->persist($parent);
$this->_em->flush();

$parent = $this->_em->find(GH10348ParentEntity::class, $parent->getId());

$this->_em->remove($parent);

$this->assertNull($this->_em->flush());
}
}

0 comments on commit 2abff4c

Please sign in to comment.