|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use Doctrine\Common\Collections\Collection; |
|
8
|
|
|
use Doctrine\ORM\Annotation as ORM; |
|
9
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
|
10
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
|
11
|
|
|
|
|
12
|
|
|
final class GH7162Test extends OrmFunctionalTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function setUp(): void |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
$this->setUpEntitySchema([ |
|
18
|
|
|
GH7162Parent::class, |
|
19
|
|
|
GH7162Child::class, |
|
20
|
|
|
]); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @group 7067 |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testIssueWithDetachedEntity(): void |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
// Create a parent without children |
|
29
|
|
|
$parent = new GH7162Parent(); |
|
30
|
|
|
$this->em->persist($parent); |
|
31
|
|
|
$this->em->flush(); |
|
32
|
|
|
$this->em->clear(); |
|
33
|
|
|
|
|
34
|
|
|
$parentId = $parent->id; |
|
35
|
|
|
|
|
36
|
|
|
// Fetch the parent as a non-cached entity |
|
37
|
|
|
/** @var GH7162Parent $parent */ |
|
38
|
|
|
$parent = $this->em->find(GH7162Parent::class, $parentId); |
|
39
|
|
|
|
|
40
|
|
|
// Create a new child and add it to the persistent collection |
|
41
|
|
|
// of children: $parent->children |
|
42
|
|
|
$child1 = new GH7162Child(); |
|
43
|
|
|
$parent->addChild($child1); |
|
44
|
|
|
|
|
45
|
|
|
// Then, remove the same child, causing an orphan removal |
|
46
|
|
|
$parent->removeChild($child1); |
|
47
|
|
|
|
|
48
|
|
|
$caughtException = null; |
|
49
|
|
|
$message = ''; |
|
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$this->em->flush(); |
|
52
|
|
|
} catch (ORMInvalidArgumentException $exception) { |
|
53
|
|
|
$message = $exception->getMessage(); |
|
|
|
|
|
|
54
|
|
|
$caughtException = $exception; |
|
55
|
|
|
} |
|
56
|
|
|
$this->em->clear(); |
|
57
|
|
|
|
|
58
|
|
|
self::assertNull( |
|
59
|
|
|
$caughtException, |
|
60
|
|
|
'Child entity is detached and should not be scheduled for orphan removal, but it is.' |
|
61
|
|
|
. ' ' |
|
62
|
|
|
. 'Message: ' |
|
63
|
|
|
. $message |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @ORM\Entity |
|
70
|
|
|
*/ |
|
71
|
|
|
class GH7162Parent |
|
72
|
|
|
{ |
|
73
|
|
|
/** |
|
74
|
|
|
* @ORM\Id |
|
75
|
|
|
* @ORM\GeneratedValue |
|
76
|
|
|
* @ORM\Column(type="integer") |
|
77
|
|
|
* |
|
78
|
|
|
* @var int |
|
79
|
|
|
*/ |
|
80
|
|
|
public $id; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @ORM\OneToMany(targetEntity=GH7162Child::class, mappedBy="parent", cascade={"remove","persist"}, orphanRemoval=true) |
|
84
|
|
|
* |
|
85
|
|
|
* @var GH7162Child[]|Collection |
|
86
|
|
|
*/ |
|
87
|
|
|
public $children; |
|
88
|
|
|
|
|
89
|
|
|
public function __construct() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->children = new ArrayCollection(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param GH7162Child $child |
|
96
|
|
|
*/ |
|
97
|
|
|
public function addChild(GH7162Child $child): void |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->children->contains($child)) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->children->add($child); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param GH7162Child $child |
|
108
|
|
|
*/ |
|
109
|
|
|
public function removeChild(GH7162Child $child): void |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
if (!$this->children->contains($child)) { |
|
|
|
|
|
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->children->removeElement($child); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @ORM\Entity |
|
121
|
|
|
*/ |
|
122
|
|
|
class GH7162Child |
|
123
|
|
|
{ |
|
124
|
|
|
/** |
|
125
|
|
|
* @ORM\Id |
|
126
|
|
|
* @ORM\GeneratedValue |
|
127
|
|
|
* @ORM\Column(type="integer") |
|
128
|
|
|
* |
|
129
|
|
|
* @var int |
|
130
|
|
|
*/ |
|
131
|
|
|
public $id; |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @ORM\ManyToOne(targetEntity=GH7162Parent::class, inversedBy="children") |
|
135
|
|
|
* @ORM\JoinColumn(referencedColumnName="parent_id") |
|
136
|
|
|
* |
|
137
|
|
|
* @var GH7162Parent |
|
138
|
|
|
*/ |
|
139
|
|
|
public $parent; |
|
140
|
|
|
} |
|
141
|
|
|
|