|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ORM; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Common\Collections\AbstractLazyCollection; |
|
23
|
|
|
use Doctrine\Common\Collections\Collection; |
|
24
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
25
|
|
|
use Doctrine\Common\Collections\Selectable; |
|
26
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
27
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* A PersistentCollection represents a collection of elements that have persistent state. |
|
31
|
|
|
* |
|
32
|
|
|
* Collections of entities represent only the associations (links) to those entities. |
|
33
|
|
|
* That means, if the collection is part of a many-many mapping and you remove |
|
34
|
|
|
* entities from the collection, only the links in the relation table are removed (on flush). |
|
35
|
|
|
* Similarly, if you remove entities from a collection that is part of a one-many |
|
36
|
|
|
* mapping this will only result in the nulling out of the foreign keys on flush. |
|
37
|
|
|
* |
|
38
|
|
|
* @since 2.0 |
|
39
|
|
|
* @author Konsta Vesterinen <[email protected]> |
|
40
|
|
|
* @author Roman Borschel <[email protected]> |
|
41
|
|
|
* @author Giorgio Sironi <[email protected]> |
|
42
|
|
|
* @author Stefano Rodriguez <[email protected]> |
|
43
|
|
|
*/ |
|
44
|
|
|
final class PersistentCollection extends AbstractLazyCollection implements Selectable |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* A snapshot of the collection at the moment it was fetched from the database. |
|
48
|
|
|
* This is used to create a diff of the collection at commit time. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $snapshot = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The entity that owns this collection. |
|
56
|
|
|
* |
|
57
|
|
|
* @var object |
|
58
|
|
|
*/ |
|
59
|
|
|
private $owner; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The association mapping the collection belongs to. |
|
63
|
|
|
* This is currently either a OneToManyMapping or a ManyToManyMapping. |
|
64
|
|
|
* |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
private $association; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The EntityManager that manages the persistence of the collection. |
|
71
|
|
|
* |
|
72
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
private $em; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The name of the field on the target entities that points to the owner |
|
78
|
|
|
* of the collection. This is only set if the association is bi-directional. |
|
79
|
|
|
* |
|
80
|
|
|
* @var string |
|
81
|
|
|
*/ |
|
82
|
|
|
private $backRefFieldName; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The class descriptor of the collection's entity type. |
|
86
|
|
|
* |
|
87
|
|
|
* @var ClassMetadata |
|
88
|
|
|
*/ |
|
89
|
|
|
private $typeClass; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Whether the collection is dirty and needs to be synchronized with the database |
|
93
|
|
|
* when the UnitOfWork that manages its persistent state commits. |
|
94
|
|
|
* |
|
95
|
|
|
* @var boolean |
|
96
|
|
|
*/ |
|
97
|
|
|
private $isDirty = false; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Creates a new persistent collection. |
|
101
|
|
|
* |
|
102
|
|
|
* @param EntityManagerInterface $em The EntityManager the collection will be associated with. |
|
103
|
|
|
* @param ClassMetadata $class The class descriptor of the entity type of this collection. |
|
104
|
|
|
* @param Collection $collection The collection elements. |
|
105
|
|
|
*/ |
|
106
|
889 |
|
public function __construct(EntityManagerInterface $em, $class, Collection $collection) |
|
107
|
|
|
{ |
|
108
|
889 |
|
$this->collection = $collection; |
|
109
|
889 |
|
$this->em = $em; |
|
110
|
889 |
|
$this->typeClass = $class; |
|
111
|
889 |
|
$this->initialized = true; |
|
112
|
889 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* INTERNAL: |
|
116
|
|
|
* Sets the collection's owning entity together with the AssociationMapping that |
|
117
|
|
|
* describes the association between the owner and the elements of the collection. |
|
118
|
|
|
* |
|
119
|
|
|
* @param object $entity |
|
120
|
|
|
* @param array $assoc |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
883 |
|
public function setOwner($entity, array $assoc) |
|
125
|
|
|
{ |
|
126
|
883 |
|
$this->owner = $entity; |
|
127
|
883 |
|
$this->association = $assoc; |
|
128
|
883 |
|
$this->backRefFieldName = $assoc['inversedBy'] ?: $assoc['mappedBy']; |
|
129
|
883 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* INTERNAL: |
|
133
|
|
|
* Gets the collection owner. |
|
134
|
|
|
* |
|
135
|
|
|
* @return object |
|
136
|
|
|
*/ |
|
137
|
525 |
|
public function getOwner() |
|
138
|
|
|
{ |
|
139
|
525 |
|
return $this->owner; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return Mapping\ClassMetadata |
|
144
|
|
|
*/ |
|
145
|
21 |
|
public function getTypeClass() |
|
146
|
|
|
{ |
|
147
|
21 |
|
return $this->typeClass; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* INTERNAL: |
|
152
|
|
|
* Adds an element to a collection during hydration. This will automatically |
|
153
|
|
|
* complete bidirectional associations in the case of a one-to-many association. |
|
154
|
|
|
* |
|
155
|
|
|
* @param mixed $element The element to add. |
|
156
|
|
|
* |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
157 |
|
public function hydrateAdd($element) |
|
160
|
|
|
{ |
|
161
|
157 |
|
$this->collection->add($element); |
|
162
|
|
|
|
|
163
|
|
|
// If _backRefFieldName is set and its a one-to-many association, |
|
164
|
|
|
// we need to set the back reference. |
|
165
|
157 |
|
if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) { |
|
166
|
|
|
// Set back reference to owner |
|
167
|
100 |
|
$this->typeClass->reflFields[$this->backRefFieldName]->setValue( |
|
168
|
100 |
|
$element, $this->owner |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
100 |
|
$this->em->getUnitOfWork()->setOriginalEntityProperty( |
|
172
|
100 |
|
spl_object_hash($element), $this->backRefFieldName, $this->owner |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
157 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* INTERNAL: |
|
179
|
|
|
* Sets a keyed element in the collection during hydration. |
|
180
|
|
|
* |
|
181
|
|
|
* @param mixed $key The key to set. |
|
182
|
|
|
* @param mixed $element The element to set. |
|
183
|
|
|
* |
|
184
|
|
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
39 |
|
public function hydrateSet($key, $element) |
|
187
|
|
|
{ |
|
188
|
39 |
|
$this->collection->set($key, $element); |
|
189
|
|
|
|
|
190
|
|
|
// If _backRefFieldName is set, then the association is bidirectional |
|
191
|
|
|
// and we need to set the back reference. |
|
192
|
39 |
|
if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) { |
|
193
|
|
|
// Set back reference to owner |
|
194
|
23 |
|
$this->typeClass->reflFields[$this->backRefFieldName]->setValue( |
|
195
|
23 |
|
$element, $this->owner |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
39 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Initializes the collection by loading its contents from the database |
|
202
|
|
|
* if the collection is not yet initialized. |
|
203
|
|
|
* |
|
204
|
|
|
* @return void |
|
205
|
|
|
*/ |
|
206
|
766 |
|
public function initialize() |
|
207
|
|
|
{ |
|
208
|
766 |
|
if ($this->initialized || ! $this->association) { |
|
|
|
|
|
|
209
|
744 |
|
return; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
146 |
|
$this->doInitialize(); |
|
213
|
|
|
|
|
214
|
146 |
|
$this->initialized = true; |
|
215
|
146 |
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* INTERNAL: |
|
219
|
|
|
* Tells this collection to take a snapshot of its current state. |
|
220
|
|
|
* |
|
221
|
|
|
* @return void |
|
222
|
|
|
*/ |
|
223
|
587 |
|
public function takeSnapshot() |
|
224
|
|
|
{ |
|
225
|
587 |
|
$this->snapshot = $this->collection->toArray(); |
|
226
|
587 |
|
$this->isDirty = false; |
|
227
|
587 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* INTERNAL: |
|
231
|
|
|
* Returns the last snapshot of the elements in the collection. |
|
232
|
|
|
* |
|
233
|
|
|
* @return array The last snapshot of the elements. |
|
234
|
|
|
*/ |
|
235
|
24 |
|
public function getSnapshot() |
|
236
|
|
|
{ |
|
237
|
24 |
|
return $this->snapshot; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* INTERNAL: |
|
242
|
|
|
* getDeleteDiff |
|
243
|
|
|
* |
|
244
|
|
|
* @return array |
|
245
|
|
|
*/ |
|
246
|
328 |
View Code Duplication |
public function getDeleteDiff() |
|
247
|
|
|
{ |
|
248
|
328 |
|
return array_udiff_assoc( |
|
249
|
328 |
|
$this->snapshot, |
|
250
|
328 |
|
$this->collection->toArray(), |
|
251
|
|
|
function($a, $b) { return $a === $b ? 0 : 1; } |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* INTERNAL: |
|
257
|
|
|
* getInsertDiff |
|
258
|
|
|
* |
|
259
|
|
|
* @return array |
|
260
|
|
|
*/ |
|
261
|
328 |
View Code Duplication |
public function getInsertDiff() |
|
262
|
|
|
{ |
|
263
|
328 |
|
return array_udiff_assoc( |
|
264
|
328 |
|
$this->collection->toArray(), |
|
265
|
328 |
|
$this->snapshot, |
|
266
|
|
|
function($a, $b) { return $a === $b ? 0 : 1; } |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* INTERNAL: Gets the association mapping of the collection. |
|
272
|
|
|
* |
|
273
|
|
|
* @return array |
|
274
|
|
|
*/ |
|
275
|
553 |
|
public function getMapping() |
|
276
|
|
|
{ |
|
277
|
553 |
|
return $this->association; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Marks this collection as changed/dirty. |
|
282
|
|
|
* |
|
283
|
|
|
* @return void |
|
284
|
|
|
*/ |
|
285
|
153 |
|
private function changed() |
|
286
|
|
|
{ |
|
287
|
153 |
|
if ($this->isDirty) { |
|
288
|
78 |
|
return; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
153 |
|
$this->isDirty = true; |
|
292
|
|
|
|
|
293
|
153 |
|
if ($this->association !== null && |
|
294
|
153 |
|
$this->association['isOwningSide'] && |
|
295
|
153 |
|
$this->association['type'] === ClassMetadata::MANY_TO_MANY && |
|
296
|
153 |
|
$this->owner && |
|
297
|
153 |
|
$this->em->getClassMetadata(get_class($this->owner))->isChangeTrackingNotify()) { |
|
298
|
1 |
|
$this->em->getUnitOfWork()->scheduleForDirtyCheck($this->owner); |
|
299
|
|
|
} |
|
300
|
153 |
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Gets a boolean flag indicating whether this collection is dirty which means |
|
304
|
|
|
* its state needs to be synchronized with the database. |
|
305
|
|
|
* |
|
306
|
|
|
* @return boolean TRUE if the collection is dirty, FALSE otherwise. |
|
307
|
|
|
*/ |
|
308
|
801 |
|
public function isDirty() |
|
309
|
|
|
{ |
|
310
|
801 |
|
return $this->isDirty; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Sets a boolean flag, indicating whether this collection is dirty. |
|
315
|
|
|
* |
|
316
|
|
|
* @param boolean $dirty Whether the collection should be marked dirty or not. |
|
317
|
|
|
* |
|
318
|
|
|
* @return void |
|
319
|
|
|
*/ |
|
320
|
789 |
|
public function setDirty($dirty) |
|
321
|
|
|
{ |
|
322
|
789 |
|
$this->isDirty = $dirty; |
|
323
|
789 |
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Sets the initialized flag of the collection, forcing it into that state. |
|
327
|
|
|
* |
|
328
|
|
|
* @param boolean $bool |
|
329
|
|
|
* |
|
330
|
|
|
* @return void |
|
331
|
|
|
*/ |
|
332
|
537 |
|
public function setInitialized($bool) |
|
333
|
|
|
{ |
|
334
|
537 |
|
$this->initialized = $bool; |
|
335
|
537 |
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* {@inheritdoc} |
|
339
|
|
|
*/ |
|
340
|
16 |
View Code Duplication |
public function remove($key) |
|
341
|
|
|
{ |
|
342
|
|
|
// TODO: If the keys are persistent as well (not yet implemented) |
|
343
|
|
|
// and the collection is not initialized and orphanRemoval is |
|
344
|
|
|
// not used we can issue a straight SQL delete/update on the |
|
345
|
|
|
// association (table). Without initializing the collection. |
|
346
|
16 |
|
$removed = parent::remove($key); |
|
347
|
|
|
|
|
348
|
16 |
|
if ( ! $removed) { |
|
349
|
|
|
return $removed; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
16 |
|
$this->changed(); |
|
353
|
|
|
|
|
354
|
16 |
|
if ($this->association !== null && |
|
355
|
16 |
|
$this->association['type'] & ClassMetadata::TO_MANY && |
|
356
|
16 |
|
$this->owner && |
|
357
|
16 |
|
$this->association['orphanRemoval']) { |
|
358
|
4 |
|
$this->em->getUnitOfWork()->scheduleOrphanRemoval($removed); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
16 |
|
return $removed; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* {@inheritdoc} |
|
366
|
|
|
*/ |
|
367
|
26 |
|
public function removeElement($element) |
|
368
|
|
|
{ |
|
369
|
26 |
|
if ( ! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) { |
|
370
|
13 |
|
if ($this->collection->contains($element)) { |
|
371
|
|
|
return $this->collection->removeElement($element); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
13 |
|
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association); |
|
375
|
|
|
|
|
376
|
13 |
|
return $persister->removeElement($this, $element); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
13 |
|
$removed = parent::removeElement($element); |
|
380
|
|
|
|
|
381
|
13 |
|
if ( ! $removed) { |
|
382
|
|
|
return $removed; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
13 |
|
$this->changed(); |
|
386
|
|
|
|
|
387
|
13 |
|
if ($this->association !== null && |
|
388
|
13 |
|
$this->association['type'] & ClassMetadata::TO_MANY && |
|
389
|
13 |
|
$this->owner && |
|
390
|
13 |
|
$this->association['orphanRemoval']) { |
|
391
|
4 |
|
$this->em->getUnitOfWork()->scheduleOrphanRemoval($element); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
13 |
|
return $removed; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* {@inheritdoc} |
|
399
|
|
|
*/ |
|
400
|
28 |
View Code Duplication |
public function containsKey($key) |
|
401
|
|
|
{ |
|
402
|
28 |
|
if (! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY |
|
403
|
28 |
|
&& isset($this->association['indexBy'])) { |
|
404
|
11 |
|
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association); |
|
405
|
|
|
|
|
406
|
11 |
|
return $this->collection->containsKey($key) || $persister->containsKey($this, $key); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
17 |
|
return parent::containsKey($key); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* {@inheritdoc} |
|
414
|
|
|
*/ |
|
415
|
34 |
View Code Duplication |
public function contains($element) |
|
416
|
|
|
{ |
|
417
|
34 |
|
if ( ! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) { |
|
418
|
17 |
|
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association); |
|
419
|
|
|
|
|
420
|
17 |
|
return $this->collection->contains($element) || $persister->contains($this, $element); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
17 |
|
return parent::contains($element); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* {@inheritdoc} |
|
428
|
|
|
*/ |
|
429
|
86 |
|
public function get($key) |
|
430
|
|
|
{ |
|
431
|
86 |
|
if ( ! $this->initialized |
|
432
|
86 |
|
&& $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY |
|
433
|
86 |
|
&& isset($this->association['indexBy']) |
|
434
|
|
|
) { |
|
435
|
5 |
|
if (!$this->typeClass->isIdentifierComposite && $this->typeClass->isIdentifier($this->association['indexBy'])) { |
|
436
|
1 |
|
return $this->em->find($this->typeClass->name, $key); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
4 |
|
return $this->em->getUnitOfWork()->getCollectionPersister($this->association)->get($this, $key); |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
81 |
|
return parent::get($key); |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/** |
|
446
|
|
|
* {@inheritdoc} |
|
447
|
|
|
*/ |
|
448
|
734 |
|
public function count() |
|
449
|
|
|
{ |
|
450
|
734 |
|
if ( ! $this->initialized && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) { |
|
451
|
32 |
|
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association); |
|
452
|
|
|
|
|
453
|
32 |
|
return $persister->count($this) + ($this->isDirty ? $this->collection->count() : 0); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
728 |
|
return parent::count(); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* {@inheritdoc} |
|
461
|
|
|
*/ |
|
462
|
4 |
View Code Duplication |
public function set($key, $value) |
|
463
|
|
|
{ |
|
464
|
4 |
|
parent::set($key, $value); |
|
465
|
|
|
|
|
466
|
4 |
|
$this->changed(); |
|
467
|
|
|
|
|
468
|
4 |
|
if (is_object($value) && $this->em) { |
|
469
|
4 |
|
$this->em->getUnitOfWork()->cancelOrphanRemoval($value); |
|
470
|
|
|
} |
|
471
|
4 |
|
} |
|
472
|
|
|
|
|
473
|
|
|
/** |
|
474
|
|
|
* {@inheritdoc} |
|
475
|
|
|
*/ |
|
476
|
117 |
View Code Duplication |
public function add($value) |
|
477
|
|
|
{ |
|
478
|
117 |
|
$this->collection->add($value); |
|
479
|
|
|
|
|
480
|
117 |
|
$this->changed(); |
|
481
|
|
|
|
|
482
|
117 |
|
if (is_object($value) && $this->em) { |
|
483
|
116 |
|
$this->em->getUnitOfWork()->cancelOrphanRemoval($value); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
117 |
|
return true; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/* ArrayAccess implementation */ |
|
490
|
|
|
|
|
491
|
|
|
/** |
|
492
|
|
|
* {@inheritdoc} |
|
493
|
|
|
*/ |
|
494
|
17 |
|
public function offsetExists($offset) |
|
495
|
|
|
{ |
|
496
|
17 |
|
return $this->containsKey($offset); |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
/** |
|
500
|
|
|
* {@inheritdoc} |
|
501
|
|
|
*/ |
|
502
|
62 |
|
public function offsetGet($offset) |
|
503
|
|
|
{ |
|
504
|
62 |
|
return $this->get($offset); |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
|
|
/** |
|
508
|
|
|
* {@inheritdoc} |
|
509
|
|
|
*/ |
|
510
|
96 |
|
public function offsetSet($offset, $value) |
|
511
|
|
|
{ |
|
512
|
96 |
|
if ( ! isset($offset)) { |
|
513
|
96 |
|
$this->add($value); |
|
514
|
96 |
|
return; |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
$this->set($offset, $value); |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* {@inheritdoc} |
|
522
|
|
|
*/ |
|
523
|
6 |
|
public function offsetUnset($offset) |
|
524
|
|
|
{ |
|
525
|
6 |
|
return $this->remove($offset); |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* {@inheritdoc} |
|
530
|
|
|
*/ |
|
531
|
790 |
|
public function isEmpty() |
|
532
|
|
|
{ |
|
533
|
790 |
|
return $this->collection->isEmpty() && $this->count() === 0; |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
/** |
|
537
|
|
|
* {@inheritdoc} |
|
538
|
|
|
*/ |
|
539
|
21 |
|
public function clear() |
|
540
|
|
|
{ |
|
541
|
21 |
|
if ($this->initialized && $this->isEmpty()) { |
|
542
|
1 |
|
$this->collection->clear(); |
|
543
|
|
|
|
|
544
|
1 |
|
return; |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
20 |
|
$uow = $this->em->getUnitOfWork(); |
|
548
|
|
|
|
|
549
|
20 |
|
if ($this->association['type'] & ClassMetadata::TO_MANY && |
|
550
|
20 |
|
$this->association['orphanRemoval'] && |
|
551
|
20 |
|
$this->owner) { |
|
552
|
|
|
// we need to initialize here, as orphan removal acts like implicit cascadeRemove, |
|
553
|
|
|
// hence for event listeners we need the objects in memory. |
|
554
|
6 |
|
$this->initialize(); |
|
555
|
|
|
|
|
556
|
6 |
|
foreach ($this->collection as $element) { |
|
557
|
6 |
|
$uow->scheduleOrphanRemoval($element); |
|
558
|
|
|
} |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
20 |
|
$this->collection->clear(); |
|
562
|
|
|
|
|
563
|
20 |
|
$this->initialized = true; // direct call, {@link initialize()} is too expensive |
|
564
|
|
|
|
|
565
|
20 |
|
if ($this->association['isOwningSide'] && $this->owner) { |
|
566
|
14 |
|
$this->changed(); |
|
567
|
|
|
|
|
568
|
14 |
|
$uow->scheduleCollectionDeletion($this); |
|
569
|
|
|
|
|
570
|
14 |
|
$this->takeSnapshot(); |
|
571
|
|
|
} |
|
572
|
20 |
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* Called by PHP when this collection is serialized. Ensures that only the |
|
576
|
|
|
* elements are properly serialized. |
|
577
|
|
|
* |
|
578
|
|
|
* Internal note: Tried to implement Serializable first but that did not work well |
|
579
|
|
|
* with circular references. This solution seems simpler and works well. |
|
580
|
|
|
* |
|
581
|
|
|
* @return array |
|
582
|
|
|
*/ |
|
583
|
2 |
|
public function __sleep() |
|
584
|
|
|
{ |
|
585
|
2 |
|
return ['collection', 'initialized']; |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
/** |
|
589
|
|
|
* Extracts a slice of $length elements starting at position $offset from the Collection. |
|
590
|
|
|
* |
|
591
|
|
|
* If $length is null it returns all elements from $offset to the end of the Collection. |
|
592
|
|
|
* Keys have to be preserved by this method. Calling this method will only return the |
|
593
|
|
|
* selected slice and NOT change the elements contained in the collection slice is called on. |
|
594
|
|
|
* |
|
595
|
|
|
* @param int $offset |
|
596
|
|
|
* @param int|null $length |
|
597
|
|
|
* |
|
598
|
|
|
* @return array |
|
599
|
|
|
*/ |
|
600
|
15 |
View Code Duplication |
public function slice($offset, $length = null) |
|
601
|
|
|
{ |
|
602
|
15 |
|
if ( ! $this->initialized && ! $this->isDirty && $this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) { |
|
603
|
13 |
|
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association); |
|
604
|
|
|
|
|
605
|
13 |
|
return $persister->slice($this, $offset, $length); |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
2 |
|
return parent::slice($offset, $length); |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
/** |
|
612
|
|
|
* Cleans up internal state of cloned persistent collection. |
|
613
|
|
|
* |
|
614
|
|
|
* The following problems have to be prevented: |
|
615
|
|
|
* 1. Added entities are added to old PC |
|
616
|
|
|
* 2. New collection is not dirty, if reused on other entity nothing |
|
617
|
|
|
* changes. |
|
618
|
|
|
* 3. Snapshot leads to invalid diffs being generated. |
|
619
|
|
|
* 4. Lazy loading grabs entities from old owner object. |
|
620
|
|
|
* 5. New collection is connected to old owner and leads to duplicate keys. |
|
621
|
|
|
* |
|
622
|
|
|
* @return void |
|
623
|
|
|
*/ |
|
624
|
11 |
|
public function __clone() |
|
625
|
|
|
{ |
|
626
|
11 |
|
if (is_object($this->collection)) { |
|
627
|
11 |
|
$this->collection = clone $this->collection; |
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
11 |
|
$this->initialize(); |
|
631
|
|
|
|
|
632
|
11 |
|
$this->owner = null; |
|
633
|
11 |
|
$this->snapshot = []; |
|
634
|
|
|
|
|
635
|
11 |
|
$this->changed(); |
|
636
|
11 |
|
} |
|
637
|
|
|
|
|
638
|
|
|
/** |
|
639
|
|
|
* Selects all elements from a selectable that match the expression and |
|
640
|
|
|
* return a new collection containing these elements. |
|
641
|
|
|
* |
|
642
|
|
|
* @param \Doctrine\Common\Collections\Criteria $criteria |
|
643
|
|
|
* |
|
644
|
|
|
* @return Collection |
|
645
|
|
|
* |
|
646
|
|
|
* @throws \RuntimeException |
|
647
|
|
|
*/ |
|
648
|
15 |
|
public function matching(Criteria $criteria) |
|
649
|
|
|
{ |
|
650
|
15 |
|
if ($this->isDirty) { |
|
651
|
3 |
|
$this->initialize(); |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
15 |
|
if ($this->initialized) { |
|
655
|
3 |
|
return $this->collection->matching($criteria); |
|
|
|
|
|
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
12 |
|
if ($this->association['type'] === ClassMetadata::MANY_TO_MANY) { |
|
659
|
7 |
|
$persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association); |
|
660
|
|
|
|
|
661
|
7 |
|
return new ArrayCollection($persister->loadCriteria($this, $criteria)); |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
5 |
|
$builder = Criteria::expr(); |
|
665
|
5 |
|
$ownerExpression = $builder->eq($this->backRefFieldName, $this->owner); |
|
666
|
5 |
|
$expression = $criteria->getWhereExpression(); |
|
667
|
5 |
|
$expression = $expression ? $builder->andX($expression, $ownerExpression) : $ownerExpression; |
|
668
|
|
|
|
|
669
|
5 |
|
$criteria = clone $criteria; |
|
670
|
5 |
|
$criteria->where($expression); |
|
671
|
|
|
|
|
672
|
5 |
|
$persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']); |
|
673
|
|
|
|
|
674
|
5 |
|
return ($this->association['fetch'] === ClassMetadata::FETCH_EXTRA_LAZY) |
|
675
|
2 |
|
? new LazyCriteriaCollection($persister, $criteria) |
|
676
|
5 |
|
: new ArrayCollection($persister->loadCriteria($criteria)); |
|
677
|
|
|
} |
|
678
|
|
|
|
|
679
|
|
|
/** |
|
680
|
|
|
* Retrieves the wrapped Collection instance. |
|
681
|
|
|
* |
|
682
|
|
|
* @return \Doctrine\Common\Collections\Collection |
|
683
|
|
|
*/ |
|
684
|
794 |
|
public function unwrap() |
|
685
|
|
|
{ |
|
686
|
794 |
|
return $this->collection; |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
/** |
|
690
|
|
|
* {@inheritdoc} |
|
691
|
|
|
*/ |
|
692
|
146 |
|
protected function doInitialize() |
|
693
|
|
|
{ |
|
694
|
|
|
// Has NEW objects added through add(). Remember them. |
|
695
|
146 |
|
$newlyAddedDirtyObjects = []; |
|
696
|
|
|
|
|
697
|
146 |
|
if ($this->isDirty) { |
|
698
|
17 |
|
$newlyAddedDirtyObjects = $this->collection->toArray(); |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
146 |
|
$this->collection->clear(); |
|
702
|
146 |
|
$this->em->getUnitOfWork()->loadCollection($this); |
|
703
|
146 |
|
$this->takeSnapshot(); |
|
704
|
|
|
|
|
705
|
146 |
|
if ($newlyAddedDirtyObjects) { |
|
|
|
|
|
|
706
|
17 |
|
$this->restoreNewObjectsInDirtyCollection($newlyAddedDirtyObjects); |
|
707
|
|
|
} |
|
708
|
146 |
|
} |
|
709
|
|
|
|
|
710
|
|
|
/** |
|
711
|
|
|
* @param object[] $newObjects |
|
712
|
|
|
* |
|
713
|
|
|
* Note: the only reason why this entire looping/complexity is performed via `spl_object_hash` |
|
714
|
|
|
* is because we want to prevent using `array_udiff()`, which is likely to cause very |
|
715
|
|
|
* high overhead (complexity of O(n^2)). `array_diff_key()` performs the operation in |
|
716
|
|
|
* core, which is faster than using a callback for comparisons |
|
717
|
|
|
*/ |
|
718
|
17 |
|
private function restoreNewObjectsInDirtyCollection(array $newObjects) : void |
|
719
|
|
|
{ |
|
720
|
17 |
|
$loadedObjects = $this->collection->toArray(); |
|
721
|
17 |
|
$newObjectsByOid = \array_combine(\array_map('spl_object_hash', $newObjects), $newObjects); |
|
722
|
17 |
|
$loadedObjectsByOid = \array_combine(\array_map('spl_object_hash', $loadedObjects), $loadedObjects); |
|
723
|
17 |
|
$newObjectsThatWereNotLoaded = \array_diff_key($newObjectsByOid, $loadedObjectsByOid); |
|
724
|
|
|
|
|
725
|
17 |
|
if ($newObjectsThatWereNotLoaded) { |
|
|
|
|
|
|
726
|
|
|
// Reattach NEW objects added through add(), if any. |
|
727
|
16 |
|
\array_walk($newObjectsThatWereNotLoaded, [$this->collection, 'add']); |
|
728
|
|
|
|
|
729
|
16 |
|
$this->isDirty = true; |
|
730
|
|
|
} |
|
731
|
17 |
|
} |
|
732
|
|
|
} |
|
733
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.