1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\PersistentCollection; |
7
|
|
|
use Doctrine\Tests\VerifyDeprecations; |
8
|
|
|
|
9
|
|
|
class DDC729Test extends \Doctrine\Tests\OrmFunctionalTestCase |
10
|
|
|
{ |
11
|
|
|
use VerifyDeprecations; |
12
|
|
|
|
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
try { |
18
|
|
|
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em); |
19
|
|
|
$schemaTool->createSchema( |
20
|
|
|
[ |
21
|
|
|
$this->_em->getClassMetadata(DDC729A::class), |
22
|
|
|
$this->_em->getClassMetadata(DDC729B::class), |
23
|
|
|
] |
24
|
|
|
); |
25
|
|
|
} catch(\Exception $e) { |
|
|
|
|
26
|
|
|
|
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @after */ |
31
|
|
|
public function ensureTestGeneratedDeprecationMessages() : void |
32
|
|
|
{ |
33
|
|
|
$this->assertHasDeprecationMessages(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testMergeManyToMany() |
37
|
|
|
{ |
38
|
|
|
$a = new DDC729A(); |
39
|
|
|
$b = new DDC729B(); |
40
|
|
|
$a->related[] = $b; |
41
|
|
|
|
42
|
|
|
$this->_em->persist($a); |
43
|
|
|
$this->_em->persist($b); |
44
|
|
|
$this->_em->flush(); |
45
|
|
|
$this->_em->clear(); |
46
|
|
|
$aId = $a->id; |
47
|
|
|
|
48
|
|
|
$a = new DDC729A(); |
49
|
|
|
$a->id = $aId; |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf(ArrayCollection::class, $a->related); |
52
|
|
|
|
53
|
|
|
$a = $this->_em->merge($a); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf(PersistentCollection::class, $a->related); |
56
|
|
|
|
57
|
|
|
$this->assertFalse($a->related->isInitialized(), "Collection should not be marked initialized."); |
|
|
|
|
58
|
|
|
$this->assertFalse($a->related->isDirty(), "Collection should not be marked as dirty."); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->_em->flush(); |
61
|
|
|
$this->_em->clear(); |
62
|
|
|
|
63
|
|
|
$a = $this->_em->find(DDC729A::class, $aId); |
64
|
|
|
$this->assertEquals(1, count($a->related)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testUnidirectionalMergeManyToMany() |
68
|
|
|
{ |
69
|
|
|
$a = new DDC729A(); |
70
|
|
|
$b1 = new DDC729B(); |
71
|
|
|
$b2 = new DDC729B(); |
72
|
|
|
$a->related[] = $b1; |
73
|
|
|
|
74
|
|
|
$this->_em->persist($a); |
75
|
|
|
$this->_em->persist($b1); |
76
|
|
|
$this->_em->persist($b2); |
77
|
|
|
$this->_em->flush(); |
78
|
|
|
$this->_em->clear(); |
79
|
|
|
$aId = $a->id; |
80
|
|
|
|
81
|
|
|
$a = new DDC729A(); |
82
|
|
|
$a->id = $aId; |
83
|
|
|
|
84
|
|
|
$a = $this->_em->merge($a); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$a->related->set(0, $this->_em->merge($b1)); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$a->related->set(1, $this->_em->merge($b2)); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->_em->flush(); |
91
|
|
|
$this->_em->clear(); |
92
|
|
|
|
93
|
|
|
$a = $this->_em->find(DDC729A::class, $aId); |
94
|
|
|
$this->assertEquals(2, count($a->related)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testBidirectionalMergeManyToMany() |
98
|
|
|
{ |
99
|
|
|
$a = new DDC729A(); |
100
|
|
|
$b1 = new DDC729B(); |
101
|
|
|
$b2 = new DDC729B(); |
102
|
|
|
$a->related[] = $b1; |
103
|
|
|
|
104
|
|
|
$this->_em->persist($a); |
105
|
|
|
$this->_em->persist($b1); |
106
|
|
|
$this->_em->persist($b2); |
107
|
|
|
$this->_em->flush(); |
108
|
|
|
$this->_em->clear(); |
109
|
|
|
$aId = $a->id; |
110
|
|
|
|
111
|
|
|
$a = new DDC729A(); |
112
|
|
|
$a->id = $aId; |
113
|
|
|
|
114
|
|
|
$a = $this->_em->merge($a); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$a->related->set(0, $this->_em->merge($b1)); |
|
|
|
|
117
|
|
|
$b1->related->set(0, $a); |
118
|
|
|
|
119
|
|
|
$a->related->set(1, $this->_em->merge($b2)); |
|
|
|
|
120
|
|
|
$b2->related->set(0, $a); |
121
|
|
|
|
122
|
|
|
$this->_em->flush(); |
123
|
|
|
$this->_em->clear(); |
124
|
|
|
|
125
|
|
|
$a = $this->_em->find(DDC729A::class, $aId); |
126
|
|
|
$this->assertEquals(2, count($a->related)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testBidirectionalMultiMergeManyToMany() |
130
|
|
|
{ |
131
|
|
|
$a = new DDC729A(); |
132
|
|
|
$b1 = new DDC729B(); |
133
|
|
|
$b2 = new DDC729B(); |
134
|
|
|
$a->related[] = $b1; |
135
|
|
|
|
136
|
|
|
$this->_em->persist($a); |
137
|
|
|
$this->_em->persist($b1); |
138
|
|
|
$this->_em->persist($b2); |
139
|
|
|
$this->_em->flush(); |
140
|
|
|
$this->_em->clear(); |
141
|
|
|
$aId = $a->id; |
142
|
|
|
|
143
|
|
|
$a = new DDC729A(); |
144
|
|
|
$a->id = $aId; |
145
|
|
|
|
146
|
|
|
$a = $this->_em->merge($a); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$a->related->set(0, $this->_em->merge($b1)); |
|
|
|
|
149
|
|
|
$b1->related->set(0, $this->_em->merge($a)); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$a->related->set(1, $this->_em->merge($b2)); |
|
|
|
|
152
|
|
|
$b2->related->set(0, $this->_em->merge($a)); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$this->_em->flush(); |
155
|
|
|
$this->_em->clear(); |
156
|
|
|
|
157
|
|
|
$a = $this->_em->find(DDC729A::class, $aId); |
158
|
|
|
$this->assertEquals(2, count($a->related)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @Entity |
164
|
|
|
*/ |
165
|
|
|
class DDC729A |
166
|
|
|
{ |
167
|
|
|
/** @Id @GeneratedValue @Column(type="integer") */ |
168
|
|
|
public $id; |
169
|
|
|
|
170
|
|
|
/** @ManyToMany(targetEntity="DDC729B", inversedBy="related") */ |
171
|
|
|
public $related; |
172
|
|
|
|
173
|
|
|
public function __construct() |
174
|
|
|
{ |
175
|
|
|
$this->related = new \Doctrine\Common\Collections\ArrayCollection(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @Entity |
181
|
|
|
*/ |
182
|
|
|
class DDC729B |
183
|
|
|
{ |
184
|
|
|
/** @Id @GeneratedValue @Column(type="integer") */ |
185
|
|
|
public $id; |
186
|
|
|
|
187
|
|
|
/** @ManyToMany(targetEntity="DDC729B", mappedBy="related") */ |
188
|
|
|
public $related; |
189
|
|
|
|
190
|
|
|
public function __construct() |
191
|
|
|
{ |
192
|
|
|
$this->related = new \Doctrine\Common\Collections\ArrayCollection(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|