1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Mapping; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use Doctrine\ORM\Cache\Exception\CacheException; |
9
|
|
|
use Doctrine\ORM\Cache\Exception\NonCacheableEntityAssociation; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Doctrine\ORM\Mapping\Factory\NamingStrategy; |
12
|
|
|
use Doctrine\ORM\Reflection\ReflectionService; |
13
|
|
|
use Doctrine\ORM\Sequencing\Planning\ValueGenerationPlan; |
14
|
|
|
use Doctrine\ORM\Utility\PersisterHelper; |
15
|
|
|
use ReflectionException; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use function array_diff; |
18
|
|
|
use function array_filter; |
19
|
|
|
use function array_intersect; |
20
|
|
|
use function array_map; |
21
|
|
|
use function array_merge; |
22
|
|
|
use function class_exists; |
23
|
|
|
use function count; |
24
|
|
|
use function get_class; |
25
|
|
|
use function in_array; |
26
|
|
|
use function interface_exists; |
27
|
|
|
use function is_subclass_of; |
28
|
|
|
use function method_exists; |
29
|
|
|
use function spl_object_id; |
30
|
|
|
use function sprintf; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata |
34
|
|
|
* of an entity and its associations. |
35
|
|
|
*/ |
36
|
|
|
class ClassMetadata extends ComponentMetadata implements TableOwner |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* The name of the custom repository class used for the entity class. |
40
|
|
|
* (Optional). |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $customRepositoryClassName; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* READ-ONLY: Whether this class describes the mapping of a mapped superclass. |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
public $isMappedSuperclass = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* READ-ONLY: Whether this class describes the mapping of an embeddable class. |
55
|
|
|
* |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
public $isEmbeddedClass = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Whether this class describes the mapping of a read-only class. |
62
|
|
|
* That means it is never considered for change-tracking in the UnitOfWork. |
63
|
|
|
* It is a very helpful performance optimization for entities that are immutable, |
64
|
|
|
* either in your domain or through the relation database (coming from a view, |
65
|
|
|
* or a history table for example). |
66
|
|
|
* |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
private $readOnly = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The names of all subclasses (descendants). |
73
|
|
|
* |
74
|
|
|
* @var string[] |
75
|
|
|
*/ |
76
|
|
|
protected $subClasses = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* READ-ONLY: The names of all embedded classes based on properties. |
80
|
|
|
* |
81
|
|
|
* @var string[] |
82
|
|
|
*/ |
83
|
|
|
//public $embeddedClasses = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* READ-ONLY: The registered lifecycle callbacks for entities of this class. |
87
|
|
|
* |
88
|
|
|
* @var string[][] |
89
|
|
|
*/ |
90
|
|
|
public $lifecycleCallbacks = []; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* READ-ONLY: The registered entity listeners. |
94
|
|
|
* |
95
|
|
|
* @var mixed[][] |
96
|
|
|
*/ |
97
|
|
|
public $entityListeners = []; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* READ-ONLY: The field names of all fields that are part of the identifier/primary key |
101
|
|
|
* of the mapped entity class. |
102
|
|
|
* |
103
|
|
|
* @var string[] |
104
|
|
|
*/ |
105
|
|
|
public $identifier = []; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* READ-ONLY: The inheritance mapping type used by the class. |
109
|
|
|
* |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
public $inheritanceType = InheritanceType::NONE; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* READ-ONLY: The policy used for change-tracking on entities of this class. |
116
|
|
|
* |
117
|
|
|
* @var string |
118
|
|
|
*/ |
119
|
|
|
public $changeTrackingPolicy = ChangeTrackingPolicy::DEFERRED_IMPLICIT; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* READ-ONLY: The discriminator value of this class. |
123
|
|
|
* |
124
|
|
|
* <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies |
125
|
|
|
* where a discriminator column is used.</b> |
126
|
|
|
* |
127
|
|
|
* @see discriminatorColumn |
128
|
|
|
* |
129
|
|
|
* @var mixed |
130
|
|
|
*/ |
131
|
|
|
public $discriminatorValue; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* READ-ONLY: The discriminator map of all mapped classes in the hierarchy. |
135
|
|
|
* |
136
|
|
|
* <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies |
137
|
|
|
* where a discriminator column is used.</b> |
138
|
|
|
* |
139
|
|
|
* @see discriminatorColumn |
140
|
|
|
* |
141
|
|
|
* @var string[] |
142
|
|
|
*/ |
143
|
|
|
public $discriminatorMap = []; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* READ-ONLY: The definition of the discriminator column used in JOINED and SINGLE_TABLE |
147
|
|
|
* inheritance mappings. |
148
|
|
|
* |
149
|
|
|
* @var DiscriminatorColumnMetadata |
150
|
|
|
*/ |
151
|
|
|
public $discriminatorColumn; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* READ-ONLY: The primary table metadata. |
155
|
|
|
* |
156
|
|
|
* @var TableMetadata |
157
|
|
|
*/ |
158
|
|
|
public $table; |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* READ-ONLY: An array of field names. Used to look up field names from column names. |
162
|
|
|
* Keys are column names and values are field names. |
163
|
|
|
* |
164
|
|
|
* @var string[] |
165
|
|
|
*/ |
166
|
|
|
public $fieldNames = []; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* READ-ONLY: The field which is used for versioning in optimistic locking (if any). |
170
|
|
|
* |
171
|
|
|
* @var FieldMetadata|null |
172
|
|
|
*/ |
173
|
|
|
public $versionProperty; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* NamingStrategy determining the default column and table names. |
177
|
|
|
* |
178
|
|
|
* @var NamingStrategy |
179
|
|
|
*/ |
180
|
|
|
protected $namingStrategy; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Value generation plan is responsible for generating values for auto-generated fields. |
184
|
|
|
* |
185
|
|
|
* @var ValueGenerationPlan |
186
|
|
|
*/ |
187
|
|
|
protected $valueGenerationPlan; |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Initializes a new ClassMetadata instance that will hold the object-relational mapping |
191
|
|
|
* metadata of the class with the given name. |
192
|
|
|
* |
193
|
|
|
* @param string $entityName The name of the entity class. |
194
|
|
|
* @param ClassMetadata|null $parent Optional parent class metadata. |
195
|
|
|
*/ |
196
|
461 |
|
public function __construct( |
197
|
|
|
string $entityName, |
198
|
|
|
?ComponentMetadata $parent, |
199
|
|
|
ClassMetadataBuildingContext $metadataBuildingContext |
200
|
|
|
) { |
201
|
461 |
|
parent::__construct($entityName, $metadataBuildingContext); |
202
|
|
|
|
203
|
461 |
|
$this->namingStrategy = $metadataBuildingContext->getNamingStrategy(); |
204
|
|
|
|
205
|
461 |
|
if ($parent) { |
206
|
98 |
|
$this->setParent($parent); |
207
|
|
|
} |
208
|
461 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
* |
213
|
|
|
* @throws MappingException |
214
|
|
|
*/ |
215
|
98 |
|
public function setParent(ComponentMetadata $parent) : void |
216
|
|
|
{ |
217
|
98 |
|
parent::setParent($parent); |
218
|
|
|
|
219
|
98 |
|
foreach ($parent->getPropertiesIterator() as $fieldName => $property) { |
220
|
95 |
|
$this->addInheritedProperty($property); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// @todo guilhermeblanco Assume to be a ClassMetadata temporarily until ClassMetadata split is complete. |
224
|
|
|
/** @var ClassMetadata $parent */ |
225
|
98 |
|
$this->setInheritanceType($parent->inheritanceType); |
|
|
|
|
226
|
98 |
|
$this->setIdentifier($parent->identifier); |
227
|
98 |
|
$this->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
228
|
|
|
|
229
|
98 |
|
if ($parent->discriminatorColumn) { |
230
|
70 |
|
$this->setDiscriminatorColumn($parent->discriminatorColumn); |
231
|
70 |
|
$this->setDiscriminatorMap($parent->discriminatorMap); |
232
|
|
|
} |
233
|
|
|
|
234
|
98 |
|
if ($parent->isMappedSuperclass) { |
235
|
27 |
|
$this->setCustomRepositoryClassName($parent->getCustomRepositoryClassName()); |
236
|
|
|
} |
237
|
|
|
|
238
|
98 |
|
if ($parent->cache) { |
239
|
3 |
|
$this->setCache(clone $parent->cache); |
240
|
|
|
} |
241
|
|
|
|
242
|
98 |
|
if (! empty($parent->lifecycleCallbacks)) { |
243
|
5 |
|
$this->lifecycleCallbacks = $parent->lifecycleCallbacks; |
244
|
|
|
} |
245
|
|
|
|
246
|
98 |
|
if (! empty($parent->entityListeners)) { |
247
|
7 |
|
$this->entityListeners = $parent->entityListeners; |
248
|
|
|
} |
249
|
98 |
|
} |
250
|
|
|
|
251
|
|
|
public function setClassName(string $className) |
252
|
|
|
{ |
253
|
|
|
$this->className = $className; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function getColumnsIterator() : ArrayIterator |
257
|
|
|
{ |
258
|
|
|
$iterator = parent::getColumnsIterator(); |
259
|
|
|
|
260
|
|
|
if ($this->discriminatorColumn) { |
261
|
|
|
$iterator->offsetSet($this->discriminatorColumn->getColumnName(), $this->discriminatorColumn); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $iterator; |
265
|
|
|
} |
266
|
|
|
|
267
|
11 |
|
public function getAncestorsIterator() : ArrayIterator |
268
|
|
|
{ |
269
|
11 |
|
$ancestors = new ArrayIterator(); |
270
|
11 |
|
$parent = $this; |
271
|
|
|
|
272
|
11 |
|
while (($parent = $parent->parent) !== null) { |
273
|
8 |
|
if ($parent instanceof ClassMetadata && $parent->isMappedSuperclass) { |
274
|
1 |
|
continue; |
275
|
|
|
} |
276
|
|
|
|
277
|
7 |
|
$ancestors->append($parent); |
278
|
|
|
} |
279
|
|
|
|
280
|
11 |
|
return $ancestors; |
281
|
|
|
} |
282
|
|
|
|
283
|
1260 |
|
public function getRootClassName() : string |
284
|
|
|
{ |
285
|
1260 |
|
return $this->parent instanceof ClassMetadata && ! $this->parent->isMappedSuperclass |
286
|
402 |
|
? $this->parent->getRootClassName() |
287
|
1260 |
|
: $this->className; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Handles metadata cloning nicely. |
292
|
|
|
*/ |
293
|
13 |
|
public function __clone() |
294
|
|
|
{ |
295
|
13 |
|
if ($this->cache) { |
296
|
12 |
|
$this->cache = clone $this->cache; |
297
|
|
|
} |
298
|
|
|
|
299
|
13 |
|
foreach ($this->properties as $name => $property) { |
300
|
13 |
|
$this->properties[$name] = clone $property; |
301
|
|
|
} |
302
|
13 |
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Creates a string representation of this instance. |
306
|
|
|
* |
307
|
|
|
* @return string The string representation of this instance. |
308
|
|
|
* |
309
|
|
|
* @todo Construct meaningful string representation. |
310
|
|
|
*/ |
311
|
|
|
public function __toString() |
312
|
|
|
{ |
313
|
|
|
return self::class . '@' . spl_object_id($this); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Determines which fields get serialized. |
318
|
|
|
* |
319
|
|
|
* It is only serialized what is necessary for best unserialization performance. |
320
|
|
|
* That means any metadata properties that are not set or empty or simply have |
321
|
|
|
* their default value are NOT serialized. |
322
|
|
|
* |
323
|
|
|
* Parts that are also NOT serialized because they can not be properly unserialized: |
324
|
|
|
* - reflectionClass |
325
|
|
|
* |
326
|
|
|
* @return string[] The names of all the fields that should be serialized. |
327
|
|
|
*/ |
328
|
4 |
|
public function __sleep() |
329
|
|
|
{ |
330
|
4 |
|
$serialized = []; |
331
|
|
|
|
332
|
|
|
// This metadata is always serialized/cached. |
333
|
4 |
|
$serialized = array_merge($serialized, [ |
334
|
4 |
|
'properties', |
335
|
|
|
'fieldNames', |
336
|
|
|
//'embeddedClasses', |
337
|
|
|
'identifier', |
338
|
|
|
'className', |
339
|
|
|
'parent', |
340
|
|
|
'table', |
341
|
|
|
'valueGenerationPlan', |
342
|
|
|
]); |
343
|
|
|
|
344
|
|
|
// The rest of the metadata is only serialized if necessary. |
345
|
4 |
|
if ($this->changeTrackingPolicy !== ChangeTrackingPolicy::DEFERRED_IMPLICIT) { |
346
|
|
|
$serialized[] = 'changeTrackingPolicy'; |
347
|
|
|
} |
348
|
|
|
|
349
|
4 |
|
if ($this->customRepositoryClassName) { |
350
|
1 |
|
$serialized[] = 'customRepositoryClassName'; |
351
|
|
|
} |
352
|
|
|
|
353
|
4 |
|
if ($this->inheritanceType !== InheritanceType::NONE) { |
354
|
1 |
|
$serialized[] = 'inheritanceType'; |
355
|
1 |
|
$serialized[] = 'discriminatorColumn'; |
356
|
1 |
|
$serialized[] = 'discriminatorValue'; |
357
|
1 |
|
$serialized[] = 'discriminatorMap'; |
358
|
1 |
|
$serialized[] = 'subClasses'; |
359
|
|
|
} |
360
|
|
|
|
361
|
4 |
|
if ($this->isMappedSuperclass) { |
362
|
|
|
$serialized[] = 'isMappedSuperclass'; |
363
|
|
|
} |
364
|
|
|
|
365
|
4 |
|
if ($this->isEmbeddedClass) { |
366
|
|
|
$serialized[] = 'isEmbeddedClass'; |
367
|
|
|
} |
368
|
|
|
|
369
|
4 |
|
if ($this->isVersioned()) { |
370
|
|
|
$serialized[] = 'versionProperty'; |
371
|
|
|
} |
372
|
|
|
|
373
|
4 |
|
if ($this->lifecycleCallbacks) { |
|
|
|
|
374
|
|
|
$serialized[] = 'lifecycleCallbacks'; |
375
|
|
|
} |
376
|
|
|
|
377
|
4 |
|
if ($this->entityListeners) { |
|
|
|
|
378
|
1 |
|
$serialized[] = 'entityListeners'; |
379
|
|
|
} |
380
|
|
|
|
381
|
4 |
|
if ($this->cache) { |
382
|
|
|
$serialized[] = 'cache'; |
383
|
|
|
} |
384
|
|
|
|
385
|
4 |
|
if ($this->readOnly) { |
386
|
1 |
|
$serialized[] = 'readOnly'; |
387
|
|
|
} |
388
|
|
|
|
389
|
4 |
|
return $serialized; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Restores some state that can not be serialized/unserialized. |
394
|
|
|
*/ |
395
|
1634 |
|
public function wakeupReflection(ReflectionService $reflectionService) : void |
396
|
|
|
{ |
397
|
|
|
// Restore ReflectionClass and properties |
398
|
1634 |
|
$this->reflectionClass = $reflectionService->getClass($this->className); |
399
|
|
|
|
400
|
1634 |
|
if (! $this->reflectionClass) { |
401
|
|
|
return; |
402
|
|
|
} |
403
|
|
|
|
404
|
1634 |
|
$this->className = $this->reflectionClass->getName(); |
405
|
|
|
|
406
|
1634 |
|
foreach ($this->properties as $property) { |
407
|
|
|
/** @var Property $property */ |
408
|
1633 |
|
$property->wakeupReflection($reflectionService); |
409
|
|
|
} |
410
|
1634 |
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Sets the change tracking policy used by this class. |
414
|
|
|
*/ |
415
|
104 |
|
public function setChangeTrackingPolicy(string $policy) : void |
416
|
|
|
{ |
417
|
104 |
|
$this->changeTrackingPolicy = $policy; |
418
|
104 |
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Checks whether a field is part of the identifier/primary key field(s). |
422
|
|
|
* |
423
|
|
|
* @param string $fieldName The field name. |
424
|
|
|
* |
425
|
|
|
* @return bool TRUE if the field is part of the table identifier/primary key field(s), FALSE otherwise. |
426
|
|
|
*/ |
427
|
1027 |
|
public function isIdentifier(string $fieldName) : bool |
428
|
|
|
{ |
429
|
1027 |
|
if (! $this->identifier) { |
|
|
|
|
430
|
1 |
|
return false; |
431
|
|
|
} |
432
|
|
|
|
433
|
1026 |
|
if (! $this->isIdentifierComposite()) { |
434
|
1022 |
|
return $fieldName === $this->identifier[0]; |
435
|
|
|
} |
436
|
|
|
|
437
|
93 |
|
return in_array($fieldName, $this->identifier, true); |
438
|
|
|
} |
439
|
|
|
|
440
|
1213 |
|
public function isIdentifierComposite() : bool |
441
|
|
|
{ |
442
|
1213 |
|
return isset($this->identifier[1]); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Validates Identifier. |
447
|
|
|
* |
448
|
|
|
* @throws MappingException |
449
|
|
|
*/ |
450
|
369 |
|
public function validateIdentifier() : void |
451
|
|
|
{ |
452
|
369 |
|
if ($this->isMappedSuperclass || $this->isEmbeddedClass) { |
453
|
27 |
|
return; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
// Verify & complete identifier mapping |
457
|
369 |
|
if (! $this->identifier) { |
|
|
|
|
458
|
4 |
|
throw MappingException::identifierRequired($this->className); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
$explicitlyGeneratedProperties = array_filter($this->properties, static function (Property $property) : bool { |
462
|
365 |
|
return $property instanceof FieldMetadata |
463
|
365 |
|
&& $property->isPrimaryKey() |
464
|
365 |
|
&& $property->hasValueGenerator(); |
465
|
365 |
|
}); |
466
|
|
|
|
467
|
365 |
|
if ($explicitlyGeneratedProperties && $this->isIdentifierComposite()) { |
|
|
|
|
468
|
|
|
throw MappingException::compositeKeyAssignedIdGeneratorRequired($this->className); |
469
|
|
|
} |
470
|
365 |
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Validates association targets actually exist. |
474
|
|
|
* |
475
|
|
|
* @throws MappingException |
476
|
|
|
*/ |
477
|
368 |
|
public function validateAssociations() : void |
478
|
|
|
{ |
479
|
368 |
|
array_map( |
480
|
|
|
function (Property $property) { |
481
|
368 |
|
if (! ($property instanceof AssociationMetadata)) { |
482
|
365 |
|
return; |
483
|
|
|
} |
484
|
|
|
|
485
|
249 |
|
$targetEntity = $property->getTargetEntity(); |
486
|
|
|
|
487
|
249 |
|
if (! class_exists($targetEntity)) { |
488
|
1 |
|
throw MappingException::invalidTargetEntityClass($targetEntity, $this->className, $property->getName()); |
489
|
|
|
} |
490
|
368 |
|
}, |
491
|
368 |
|
$this->properties |
492
|
|
|
); |
493
|
367 |
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Validates lifecycle callbacks. |
497
|
|
|
* |
498
|
|
|
* @throws MappingException |
499
|
|
|
*/ |
500
|
368 |
|
public function validateLifecycleCallbacks(ReflectionService $reflectionService) : void |
501
|
|
|
{ |
502
|
368 |
|
foreach ($this->lifecycleCallbacks as $callbacks) { |
503
|
|
|
/** @var array $callbacks */ |
504
|
10 |
|
foreach ($callbacks as $callbackFuncName) { |
505
|
10 |
|
if (! $reflectionService->hasPublicMethod($this->className, $callbackFuncName)) { |
506
|
1 |
|
throw MappingException::lifecycleCallbackMethodNotFound($this->className, $callbackFuncName); |
507
|
|
|
} |
508
|
|
|
} |
509
|
|
|
} |
510
|
367 |
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Validates & completes the basic mapping information for field mapping. |
514
|
|
|
* |
515
|
|
|
* @throws MappingException If something is wrong with the mapping. |
516
|
|
|
*/ |
517
|
21 |
|
protected function validateAndCompleteVersionFieldMapping(FieldMetadata $property) |
518
|
|
|
{ |
519
|
21 |
|
$this->versionProperty = $property; |
520
|
|
|
|
521
|
21 |
|
$options = $property->getOptions(); |
522
|
|
|
|
523
|
21 |
|
if (isset($options['default'])) { |
524
|
|
|
return; |
525
|
|
|
} |
526
|
|
|
|
527
|
21 |
|
if (in_array($property->getTypeName(), ['integer', 'bigint', 'smallint'], true)) { |
528
|
19 |
|
$property->setOptions(array_merge($options, ['default' => 1])); |
529
|
|
|
|
530
|
19 |
|
return; |
531
|
|
|
} |
532
|
|
|
|
533
|
3 |
|
if (in_array($property->getTypeName(), ['datetime', 'datetime_immutable', 'datetimetz', 'datetimetz_immutable'], true)) { |
534
|
2 |
|
$property->setOptions(array_merge($options, ['default' => 'CURRENT_TIMESTAMP'])); |
535
|
|
|
|
536
|
2 |
|
return; |
537
|
|
|
} |
538
|
|
|
|
539
|
1 |
|
throw MappingException::unsupportedOptimisticLockingType($property->getType()); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Validates & completes the basic mapping information that is common to all |
544
|
|
|
* association mappings (one-to-one, many-ot-one, one-to-many, many-to-many). |
545
|
|
|
* |
546
|
|
|
* @throws MappingException If something is wrong with the mapping. |
547
|
|
|
* @throws CacheException If entity is not cacheable. |
548
|
|
|
*/ |
549
|
281 |
|
protected function validateAndCompleteAssociationMapping(AssociationMetadata $property) |
550
|
|
|
{ |
551
|
281 |
|
$fieldName = $property->getName(); |
552
|
281 |
|
$targetEntity = $property->getTargetEntity(); |
553
|
|
|
|
554
|
281 |
|
if (! $targetEntity) { |
555
|
|
|
throw MappingException::missingTargetEntity($fieldName); |
556
|
|
|
} |
557
|
|
|
|
558
|
281 |
|
$property->setSourceEntity($this->className); |
559
|
281 |
|
$property->setTargetEntity($targetEntity); |
560
|
|
|
|
561
|
|
|
// Complete id mapping |
562
|
281 |
|
if ($property->isPrimaryKey()) { |
563
|
47 |
|
if ($property->isOrphanRemoval()) { |
564
|
1 |
|
throw MappingException::illegalOrphanRemovalOnIdentifierAssociation($this->className, $fieldName); |
565
|
|
|
} |
566
|
|
|
|
567
|
46 |
|
if ($property instanceof ToOneAssociationMetadata && count($property->getJoinColumns()) >= 2) { |
568
|
|
|
throw MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId( |
569
|
|
|
$property->getTargetEntity(), |
570
|
|
|
$this->className, |
571
|
|
|
$fieldName |
572
|
|
|
); |
573
|
|
|
} |
574
|
|
|
|
575
|
46 |
|
if ($this->cache && ! $property->getCache()) { |
576
|
2 |
|
throw NonCacheableEntityAssociation::fromEntityAndField($this->className, $fieldName); |
577
|
|
|
} |
578
|
|
|
|
579
|
44 |
|
if ($property instanceof ToManyAssociationMetadata) { |
580
|
1 |
|
throw MappingException::illegalToManyIdentifierAssociation($this->className, $property->getName()); |
581
|
|
|
} |
582
|
|
|
|
583
|
43 |
|
if (! in_array($property->getName(), $this->identifier, true)) { |
584
|
43 |
|
$this->identifier[] = $property->getName(); |
585
|
|
|
} |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
// Cascades |
589
|
277 |
|
$cascadeTypes = ['remove', 'persist', 'refresh']; |
590
|
277 |
|
$cascades = array_map('strtolower', $property->getCascade()); |
591
|
|
|
|
592
|
277 |
|
if (in_array('all', $cascades, true)) { |
593
|
6 |
|
$cascades = $cascadeTypes; |
594
|
|
|
} |
595
|
|
|
|
596
|
277 |
|
if (count($cascades) !== count(array_intersect($cascades, $cascadeTypes))) { |
597
|
1 |
|
$diffCascades = array_diff($cascades, array_intersect($cascades, $cascadeTypes)); |
598
|
|
|
|
599
|
1 |
|
throw MappingException::invalidCascadeOption($diffCascades, $this->className, $fieldName); |
600
|
|
|
} |
601
|
|
|
|
602
|
276 |
|
$property->setCascade($cascades); |
603
|
276 |
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Validates & completes a to-one association mapping. |
607
|
|
|
* |
608
|
|
|
* @param ToOneAssociationMetadata $property The association mapping to validate & complete. |
609
|
|
|
* |
610
|
|
|
* @throws RuntimeException |
611
|
|
|
* @throws MappingException |
612
|
|
|
*/ |
613
|
245 |
|
protected function validateAndCompleteToOneAssociationMetadata(ToOneAssociationMetadata $property) |
614
|
|
|
{ |
615
|
245 |
|
$fieldName = $property->getName(); |
616
|
|
|
|
617
|
245 |
|
if ($property->isOwningSide()) { |
618
|
241 |
|
$uniqueConstraintColumns = []; |
619
|
|
|
|
620
|
241 |
|
foreach ($property->getJoinColumns() as $joinColumn) { |
621
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
622
|
235 |
|
if ($property instanceof OneToOneAssociationMetadata && $this->inheritanceType !== InheritanceType::SINGLE_TABLE) { |
623
|
105 |
|
if (count($property->getJoinColumns()) === 1) { |
624
|
103 |
|
if (! $property->isPrimaryKey()) { |
625
|
103 |
|
$joinColumn->setUnique(true); |
626
|
|
|
} |
627
|
|
|
} else { |
628
|
2 |
|
$uniqueConstraintColumns[] = $joinColumn->getColumnName(); |
629
|
|
|
} |
630
|
|
|
} |
631
|
|
|
|
632
|
235 |
|
$this->fieldNames[$joinColumn->getColumnName()] = $fieldName; |
633
|
|
|
} |
634
|
|
|
|
635
|
241 |
|
if ($uniqueConstraintColumns) { |
636
|
2 |
|
if (! $this->table) { |
637
|
|
|
throw new RuntimeException( |
638
|
|
|
'ClassMetadata::setTable() has to be called before defining a one to one relationship.' |
639
|
|
|
); |
640
|
|
|
} |
641
|
|
|
|
642
|
2 |
|
$this->table->addUniqueConstraint( |
643
|
|
|
[ |
644
|
2 |
|
'name' => sprintf('%s_uniq', $fieldName), |
645
|
2 |
|
'columns' => $uniqueConstraintColumns, |
646
|
|
|
'options' => [], |
647
|
|
|
'flags' => [], |
648
|
|
|
] |
649
|
|
|
); |
650
|
|
|
} |
651
|
|
|
} |
652
|
|
|
|
653
|
245 |
|
if ($property->isOrphanRemoval()) { |
654
|
7 |
|
$cascades = $property->getCascade(); |
655
|
|
|
|
656
|
7 |
|
if (! in_array('remove', $cascades, true)) { |
657
|
6 |
|
$cascades[] = 'remove'; |
658
|
|
|
|
659
|
6 |
|
$property->setCascade($cascades); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
// @todo guilhermeblanco where is this used? |
663
|
|
|
// @todo guilhermeblanco Shouldn't we iterate through JoinColumns to set non-uniqueness? |
664
|
|
|
//$property->setUnique(false); |
665
|
|
|
} |
666
|
|
|
|
667
|
245 |
|
if ($property->isPrimaryKey() && ! $property->isOwningSide()) { |
668
|
1 |
|
throw MappingException::illegalInverseIdentifierAssociation($this->className, $fieldName); |
669
|
|
|
} |
670
|
244 |
|
} |
671
|
|
|
|
672
|
|
|
/** |
673
|
|
|
* Validates & completes a one-to-many association mapping. |
674
|
|
|
* |
675
|
|
|
* @param OneToManyAssociationMetadata $property The association mapping to validate & complete. |
676
|
|
|
* |
677
|
|
|
* @throws MappingException |
678
|
|
|
*/ |
679
|
118 |
|
protected function validateAndCompleteOneToManyMapping(OneToManyAssociationMetadata $property) |
680
|
|
|
{ |
681
|
|
|
// OneToMany MUST have mappedBy |
682
|
118 |
|
if (! $property->getMappedBy()) { |
683
|
|
|
throw MappingException::oneToManyRequiresMappedBy($property->getName()); |
684
|
|
|
} |
685
|
|
|
|
686
|
118 |
|
if ($property->isOrphanRemoval()) { |
687
|
19 |
|
$cascades = $property->getCascade(); |
688
|
|
|
|
689
|
19 |
|
if (! in_array('remove', $cascades, true)) { |
690
|
16 |
|
$cascades[] = 'remove'; |
691
|
|
|
|
692
|
16 |
|
$property->setCascade($cascades); |
693
|
|
|
} |
694
|
|
|
} |
695
|
118 |
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* {@inheritDoc} |
699
|
|
|
*/ |
700
|
401 |
|
public function getIdentifierFieldNames() |
701
|
|
|
{ |
702
|
401 |
|
return $this->identifier; |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
/** |
706
|
|
|
* Gets the name of the single id field. Note that this only works on |
707
|
|
|
* entity classes that have a single-field pk. |
708
|
|
|
* |
709
|
|
|
* @return string |
710
|
|
|
* |
711
|
|
|
* @throws MappingException If the class has a composite primary key. |
712
|
|
|
*/ |
713
|
151 |
|
public function getSingleIdentifierFieldName() |
714
|
|
|
{ |
715
|
151 |
|
if ($this->isIdentifierComposite()) { |
716
|
1 |
|
throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->className); |
717
|
|
|
} |
718
|
|
|
|
719
|
150 |
|
if (! isset($this->identifier[0])) { |
720
|
1 |
|
throw MappingException::noIdDefined($this->className); |
721
|
|
|
} |
722
|
|
|
|
723
|
149 |
|
return $this->identifier[0]; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* INTERNAL: |
728
|
|
|
* Sets the mapped identifier/primary key fields of this class. |
729
|
|
|
* Mainly used by the ClassMetadataFactory to assign inherited identifiers. |
730
|
|
|
* |
731
|
|
|
* @param mixed[] $identifier |
732
|
|
|
*/ |
733
|
100 |
|
public function setIdentifier(array $identifier) |
734
|
|
|
{ |
735
|
100 |
|
$this->identifier = $identifier; |
736
|
100 |
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* {@inheritDoc} |
740
|
|
|
*/ |
741
|
1053 |
|
public function getIdentifier() |
742
|
|
|
{ |
743
|
1053 |
|
return $this->identifier; |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
/** |
747
|
|
|
* {@inheritDoc} |
748
|
|
|
*/ |
749
|
189 |
|
public function hasField($fieldName) |
750
|
|
|
{ |
751
|
189 |
|
return isset($this->properties[$fieldName]) |
752
|
189 |
|
&& $this->properties[$fieldName] instanceof FieldMetadata; |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
/** |
756
|
|
|
* Returns an array with identifier column names and their corresponding ColumnMetadata. |
757
|
|
|
* |
758
|
|
|
* @return ColumnMetadata[] |
759
|
|
|
*/ |
760
|
441 |
|
public function getIdentifierColumns(EntityManagerInterface $em) : array |
761
|
|
|
{ |
762
|
441 |
|
$columns = []; |
763
|
|
|
|
764
|
441 |
|
foreach ($this->identifier as $idProperty) { |
765
|
441 |
|
$property = $this->getProperty($idProperty); |
766
|
|
|
|
767
|
441 |
|
if ($property instanceof FieldMetadata) { |
768
|
435 |
|
$columns[$property->getColumnName()] = $property; |
769
|
|
|
|
770
|
435 |
|
continue; |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
/** @var AssociationMetadata $property */ |
774
|
|
|
|
775
|
|
|
// Association defined as Id field |
776
|
25 |
|
$targetClass = $em->getClassMetadata($property->getTargetEntity()); |
777
|
|
|
|
778
|
25 |
|
if (! $property->isOwningSide()) { |
779
|
|
|
$property = $targetClass->getProperty($property->getMappedBy()); |
|
|
|
|
780
|
|
|
$targetClass = $em->getClassMetadata($property->getTargetEntity()); |
781
|
|
|
} |
782
|
|
|
|
783
|
25 |
|
$joinColumns = $property instanceof ManyToManyAssociationMetadata |
784
|
|
|
? $property->getJoinTable()->getInverseJoinColumns() |
785
|
25 |
|
: $property->getJoinColumns(); |
786
|
|
|
|
787
|
25 |
|
foreach ($joinColumns as $joinColumn) { |
788
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
789
|
25 |
|
$columnName = $joinColumn->getColumnName(); |
790
|
25 |
|
$referencedColumnName = $joinColumn->getReferencedColumnName(); |
791
|
|
|
|
792
|
25 |
|
if (! $joinColumn->getType()) { |
793
|
13 |
|
$joinColumn->setType(PersisterHelper::getTypeOfColumn($referencedColumnName, $targetClass, $em)); |
794
|
|
|
} |
795
|
|
|
|
796
|
25 |
|
$columns[$columnName] = $joinColumn; |
797
|
|
|
} |
798
|
|
|
} |
799
|
|
|
|
800
|
441 |
|
return $columns; |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* Gets the name of the primary table. |
805
|
|
|
*/ |
806
|
1567 |
|
public function getTableName() : ?string |
807
|
|
|
{ |
808
|
1567 |
|
return $this->table->getName(); |
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
/** |
812
|
|
|
* Gets primary table's schema name. |
813
|
|
|
*/ |
814
|
14 |
|
public function getSchemaName() : ?string |
815
|
|
|
{ |
816
|
14 |
|
return $this->table->getSchema(); |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
/** |
820
|
|
|
* Gets the table name to use for temporary identifier tables of this class. |
821
|
|
|
*/ |
822
|
7 |
|
public function getTemporaryIdTableName() : string |
823
|
|
|
{ |
824
|
7 |
|
$schema = $this->getSchemaName() === null |
825
|
6 |
|
? '' |
826
|
7 |
|
: $this->getSchemaName() . '_'; |
827
|
|
|
|
828
|
|
|
// replace dots with underscores because PostgreSQL creates temporary tables in a special schema |
829
|
7 |
|
return $schema . $this->getTableName() . '_id_tmp'; |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
/** |
833
|
|
|
* Sets the mapped subclasses of this class. |
834
|
|
|
* |
835
|
|
|
* @param string[] $subclasses The names of all mapped subclasses. |
836
|
|
|
* |
837
|
|
|
* @todo guilhermeblanco Only used for ClassMetadataTest. Remove if possible! |
838
|
|
|
*/ |
839
|
4 |
|
public function setSubclasses(array $subclasses) : void |
840
|
|
|
{ |
841
|
4 |
|
foreach ($subclasses as $subclass) { |
842
|
3 |
|
$this->subClasses[] = $subclass; |
843
|
|
|
} |
844
|
4 |
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* @return string[] |
848
|
|
|
*/ |
849
|
1080 |
|
public function getSubClasses() : array |
850
|
|
|
{ |
851
|
1080 |
|
return $this->subClasses; |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
/** |
855
|
|
|
* Sets the inheritance type used by the class and its subclasses. |
856
|
|
|
* |
857
|
|
|
* @param int $type |
858
|
|
|
* |
859
|
|
|
* @throws MappingException |
860
|
|
|
*/ |
861
|
120 |
|
public function setInheritanceType($type) : void |
862
|
|
|
{ |
863
|
120 |
|
if (! $this->isInheritanceType($type)) { |
864
|
|
|
throw MappingException::invalidInheritanceType($this->className, $type); |
865
|
|
|
} |
866
|
|
|
|
867
|
120 |
|
$this->inheritanceType = $type; |
868
|
120 |
|
} |
869
|
|
|
|
870
|
|
|
/** |
871
|
|
|
* Sets the override property mapping for an entity relationship. |
872
|
|
|
* |
873
|
|
|
* @throws RuntimeException |
874
|
|
|
* @throws MappingException |
875
|
|
|
* @throws CacheException |
876
|
|
|
*/ |
877
|
12 |
|
public function setPropertyOverride(Property $property) : void |
878
|
|
|
{ |
879
|
12 |
|
$fieldName = $property->getName(); |
880
|
|
|
|
881
|
12 |
|
if (! isset($this->properties[$fieldName])) { |
882
|
2 |
|
throw MappingException::invalidOverrideFieldName($this->className, $fieldName); |
883
|
|
|
} |
884
|
|
|
|
885
|
10 |
|
$originalProperty = $this->getProperty($fieldName); |
886
|
10 |
|
$originalPropertyClassName = get_class($originalProperty); |
887
|
|
|
|
888
|
|
|
// If moving from transient to persistent, assume it's a new property |
889
|
10 |
|
if ($originalPropertyClassName === TransientMetadata::class) { |
890
|
1 |
|
unset($this->properties[$fieldName]); |
891
|
|
|
|
892
|
1 |
|
$this->addProperty($property); |
893
|
|
|
|
894
|
1 |
|
return; |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
// Do not allow to change property type |
898
|
9 |
|
if ($originalPropertyClassName !== get_class($property)) { |
899
|
|
|
throw MappingException::invalidOverridePropertyType($this->className, $fieldName); |
900
|
|
|
} |
901
|
|
|
|
902
|
|
|
// Do not allow to change version property |
903
|
9 |
|
if ($originalProperty instanceof FieldMetadata && $originalProperty->isVersioned()) { |
904
|
|
|
throw MappingException::invalidOverrideVersionField($this->className, $fieldName); |
905
|
|
|
} |
906
|
|
|
|
907
|
9 |
|
unset($this->properties[$fieldName]); |
908
|
|
|
|
909
|
9 |
|
if ($property instanceof FieldMetadata) { |
910
|
|
|
// Unset defined fieldName prior to override |
911
|
5 |
|
unset($this->fieldNames[$originalProperty->getColumnName()]); |
|
|
|
|
912
|
|
|
|
913
|
|
|
// Revert what should not be allowed to change |
914
|
5 |
|
$property->setDeclaringClass($originalProperty->getDeclaringClass()); |
915
|
5 |
|
$property->setPrimaryKey($originalProperty->isPrimaryKey()); |
916
|
9 |
|
} elseif ($property instanceof AssociationMetadata) { |
917
|
|
|
// Unset all defined fieldNames prior to override |
918
|
9 |
|
if ($originalProperty instanceof ToOneAssociationMetadata && $originalProperty->isOwningSide()) { |
919
|
5 |
|
foreach ($originalProperty->getJoinColumns() as $joinColumn) { |
920
|
5 |
|
unset($this->fieldNames[$joinColumn->getColumnName()]); |
921
|
|
|
} |
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
// Override what it should be allowed to change |
925
|
9 |
|
if ($property->getInversedBy()) { |
926
|
2 |
|
$originalProperty->setInversedBy($property->getInversedBy()); |
|
|
|
|
927
|
|
|
} |
928
|
|
|
|
929
|
9 |
|
if ($property->getFetchMode() !== $originalProperty->getFetchMode()) { |
|
|
|
|
930
|
2 |
|
$originalProperty->setFetchMode($property->getFetchMode()); |
|
|
|
|
931
|
|
|
} |
932
|
|
|
|
933
|
9 |
|
if ($originalProperty instanceof ToOneAssociationMetadata && $property->getJoinColumns()) { |
|
|
|
|
934
|
5 |
|
$originalProperty->setJoinColumns($property->getJoinColumns()); |
935
|
8 |
|
} elseif ($originalProperty instanceof ManyToManyAssociationMetadata && $property->getJoinTable()) { |
|
|
|
|
936
|
4 |
|
$originalProperty->setJoinTable($property->getJoinTable()); |
937
|
|
|
} |
938
|
|
|
|
939
|
9 |
|
$property = $originalProperty; |
940
|
|
|
} |
941
|
|
|
|
942
|
9 |
|
$this->addProperty($property); |
|
|
|
|
943
|
9 |
|
} |
944
|
|
|
|
945
|
|
|
/** |
946
|
|
|
* Checks if this entity is the root in any entity-inheritance-hierarchy. |
947
|
|
|
* |
948
|
|
|
* @return bool |
949
|
|
|
*/ |
950
|
336 |
|
public function isRootEntity() |
951
|
|
|
{ |
952
|
336 |
|
return $this->className === $this->getRootClassName(); |
953
|
|
|
} |
954
|
|
|
|
955
|
|
|
/** |
956
|
|
|
* Checks whether a mapped field is inherited from a superclass. |
957
|
|
|
* |
958
|
|
|
* @param string $fieldName |
959
|
|
|
* |
960
|
|
|
* @return bool TRUE if the field is inherited, FALSE otherwise. |
961
|
|
|
*/ |
962
|
622 |
|
public function isInheritedProperty($fieldName) |
963
|
|
|
{ |
964
|
622 |
|
$declaringClass = $this->properties[$fieldName]->getDeclaringClass(); |
965
|
|
|
|
966
|
622 |
|
return $declaringClass->className !== $this->className; |
967
|
|
|
} |
968
|
|
|
|
969
|
|
|
/** |
970
|
|
|
* {@inheritdoc} |
971
|
|
|
*/ |
972
|
440 |
|
public function setTable(TableMetadata $table) : void |
973
|
|
|
{ |
974
|
440 |
|
$this->table = $table; |
975
|
|
|
|
976
|
|
|
// Make sure inherited and declared properties reflect newly defined table |
977
|
440 |
|
foreach ($this->properties as $property) { |
978
|
|
|
switch (true) { |
979
|
95 |
|
case $property instanceof FieldMetadata: |
980
|
95 |
|
$property->setTableName($property->getTableName() ?? $table->getName()); |
981
|
95 |
|
break; |
982
|
|
|
|
983
|
42 |
|
case $property instanceof ToOneAssociationMetadata: |
984
|
|
|
// Resolve association join column table names |
985
|
34 |
|
foreach ($property->getJoinColumns() as $joinColumn) { |
986
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
987
|
34 |
|
$joinColumn->setTableName($joinColumn->getTableName() ?? $table->getName()); |
988
|
|
|
} |
989
|
|
|
|
990
|
34 |
|
break; |
991
|
|
|
} |
992
|
|
|
} |
993
|
440 |
|
} |
994
|
|
|
|
995
|
|
|
/** |
996
|
|
|
* Checks whether the given type identifies an inheritance type. |
997
|
|
|
* |
998
|
|
|
* @param int $type |
999
|
|
|
* |
1000
|
|
|
* @return bool TRUE if the given type identifies an inheritance type, FALSe otherwise. |
1001
|
|
|
*/ |
1002
|
120 |
|
private function isInheritanceType($type) |
1003
|
|
|
{ |
1004
|
120 |
|
return $type === InheritanceType::NONE |
1005
|
93 |
|
|| $type === InheritanceType::SINGLE_TABLE |
1006
|
51 |
|
|| $type === InheritanceType::JOINED |
1007
|
120 |
|
|| $type === InheritanceType::TABLE_PER_CLASS; |
1008
|
|
|
} |
1009
|
|
|
|
1010
|
916 |
|
public function getColumn(string $columnName) : ?LocalColumnMetadata |
1011
|
|
|
{ |
1012
|
916 |
|
foreach ($this->properties as $property) { |
1013
|
916 |
|
if ($property instanceof LocalColumnMetadata && $property->getColumnName() === $columnName) { |
1014
|
916 |
|
return $property; |
1015
|
|
|
} |
1016
|
|
|
} |
1017
|
|
|
|
1018
|
|
|
return null; |
1019
|
|
|
} |
1020
|
|
|
|
1021
|
|
|
/** |
1022
|
|
|
* Add a property mapping. |
1023
|
|
|
* |
1024
|
|
|
* @throws RuntimeException |
1025
|
|
|
* @throws MappingException |
1026
|
|
|
* @throws CacheException |
1027
|
|
|
* @throws ReflectionException |
1028
|
|
|
*/ |
1029
|
428 |
|
public function addProperty(Property $property) : void |
1030
|
|
|
{ |
1031
|
428 |
|
$fieldName = $property->getName(); |
1032
|
|
|
|
1033
|
|
|
// Check for empty field name |
1034
|
428 |
|
if (empty($fieldName)) { |
1035
|
1 |
|
throw MappingException::missingFieldName($this->className); |
1036
|
|
|
} |
1037
|
|
|
|
1038
|
427 |
|
$property->setDeclaringClass($this); |
1039
|
|
|
|
1040
|
|
|
switch (true) { |
1041
|
427 |
|
case $property instanceof FieldMetadata: |
1042
|
410 |
|
$property->setColumnName($property->getColumnName() ?? $property->getName()); |
1043
|
|
|
|
1044
|
410 |
|
$this->fieldNames[$property->getColumnName()] = $property->getName(); |
1045
|
|
|
|
1046
|
410 |
|
if ($property->isVersioned()) { |
1047
|
21 |
|
$this->validateAndCompleteVersionFieldMapping($property); |
1048
|
|
|
} |
1049
|
|
|
|
1050
|
409 |
|
break; |
1051
|
|
|
|
1052
|
284 |
|
case $property instanceof OneToOneAssociationMetadata: |
1053
|
127 |
|
$this->validateAndCompleteAssociationMapping($property); |
1054
|
126 |
|
$this->validateAndCompleteToOneAssociationMetadata($property); |
1055
|
125 |
|
break; |
1056
|
|
|
|
1057
|
222 |
|
case $property instanceof OneToManyAssociationMetadata: |
1058
|
118 |
|
$this->validateAndCompleteAssociationMapping($property); |
1059
|
118 |
|
$this->validateAndCompleteOneToManyMapping($property); |
1060
|
118 |
|
break; |
1061
|
|
|
|
1062
|
217 |
|
case $property instanceof ManyToOneAssociationMetadata: |
1063
|
152 |
|
$this->validateAndCompleteAssociationMapping($property); |
1064
|
149 |
|
$this->validateAndCompleteToOneAssociationMetadata($property); |
1065
|
149 |
|
break; |
1066
|
|
|
|
1067
|
119 |
|
case $property instanceof ManyToManyAssociationMetadata: |
1068
|
104 |
|
$this->validateAndCompleteAssociationMapping($property); |
1069
|
103 |
|
break; |
1070
|
|
|
|
1071
|
|
|
default: |
1072
|
|
|
// Transient properties are ignored on purpose here! =) |
1073
|
29 |
|
break; |
1074
|
|
|
} |
1075
|
|
|
|
1076
|
420 |
|
if ($property->isPrimaryKey() && ! in_array($fieldName, $this->identifier, true)) { |
1077
|
396 |
|
$this->identifier[] = $fieldName; |
1078
|
|
|
} |
1079
|
|
|
|
1080
|
420 |
|
parent::addProperty($property); |
1081
|
420 |
|
} |
1082
|
|
|
|
1083
|
|
|
/** |
1084
|
|
|
* INTERNAL: |
1085
|
|
|
* Adds a property mapping without completing/validating it. |
1086
|
|
|
* This is mainly used to add inherited property mappings to derived classes. |
1087
|
|
|
*/ |
1088
|
96 |
|
public function addInheritedProperty(Property $property) |
1089
|
|
|
{ |
1090
|
96 |
|
if (isset($this->properties[$property->getName()])) { |
1091
|
1 |
|
throw MappingException::duplicateProperty($this->className, $this->getProperty($property->getName())); |
|
|
|
|
1092
|
|
|
} |
1093
|
|
|
|
1094
|
96 |
|
$declaringClass = $property->getDeclaringClass(); |
1095
|
96 |
|
$inheritedProperty = $declaringClass->isMappedSuperclass ? clone $property : $property; |
1096
|
|
|
|
1097
|
96 |
|
if ($inheritedProperty instanceof FieldMetadata) { |
1098
|
95 |
|
if (! $declaringClass->isMappedSuperclass) { |
1099
|
73 |
|
$inheritedProperty->setTableName($property->getTableName()); |
|
|
|
|
1100
|
|
|
} |
1101
|
|
|
|
1102
|
95 |
|
if ($inheritedProperty->isVersioned()) { |
1103
|
4 |
|
$this->versionProperty = $inheritedProperty; |
1104
|
|
|
} |
1105
|
|
|
|
1106
|
95 |
|
$this->fieldNames[$property->getColumnName()] = $property->getName(); |
1107
|
43 |
|
} elseif ($inheritedProperty instanceof AssociationMetadata) { |
1108
|
42 |
|
if ($declaringClass->isMappedSuperclass) { |
1109
|
10 |
|
$inheritedProperty->setSourceEntity($this->className); |
1110
|
|
|
} |
1111
|
|
|
|
1112
|
|
|
// Need to add inherited fieldNames |
1113
|
42 |
|
if ($inheritedProperty instanceof ToOneAssociationMetadata && $inheritedProperty->isOwningSide()) { |
1114
|
35 |
|
foreach ($inheritedProperty->getJoinColumns() as $joinColumn) { |
1115
|
|
|
/** @var JoinColumnMetadata $joinColumn */ |
1116
|
34 |
|
$this->fieldNames[$joinColumn->getColumnName()] = $property->getName(); |
1117
|
|
|
} |
1118
|
|
|
} |
1119
|
|
|
} |
1120
|
|
|
|
1121
|
96 |
|
$this->properties[$property->getName()] = $inheritedProperty; |
1122
|
96 |
|
} |
1123
|
|
|
|
1124
|
|
|
/** |
1125
|
|
|
* Registers a custom repository class for the entity class. |
1126
|
|
|
* |
1127
|
|
|
* @param string|null $repositoryClassName The class name of the custom mapper. |
1128
|
|
|
*/ |
1129
|
30 |
|
public function setCustomRepositoryClassName(?string $repositoryClassName) |
1130
|
|
|
{ |
1131
|
30 |
|
$this->customRepositoryClassName = $repositoryClassName; |
1132
|
30 |
|
} |
1133
|
|
|
|
1134
|
163 |
|
public function getCustomRepositoryClassName() : ?string |
1135
|
|
|
{ |
1136
|
163 |
|
return $this->customRepositoryClassName; |
1137
|
|
|
} |
1138
|
|
|
|
1139
|
|
|
/** |
1140
|
|
|
* Whether the class has any attached lifecycle listeners or callbacks for a lifecycle event. |
1141
|
|
|
* |
1142
|
|
|
* @param string $lifecycleEvent |
1143
|
|
|
* |
1144
|
|
|
* @return bool |
1145
|
|
|
*/ |
1146
|
|
|
public function hasLifecycleCallbacks($lifecycleEvent) |
1147
|
|
|
{ |
1148
|
|
|
return isset($this->lifecycleCallbacks[$lifecycleEvent]); |
1149
|
|
|
} |
1150
|
|
|
|
1151
|
|
|
/** |
1152
|
|
|
* Gets the registered lifecycle callbacks for an event. |
1153
|
|
|
* |
1154
|
|
|
* @param string $event |
1155
|
|
|
* |
1156
|
|
|
* @return string[] |
1157
|
|
|
*/ |
1158
|
|
|
public function getLifecycleCallbacks($event) : array |
1159
|
|
|
{ |
1160
|
|
|
return $this->lifecycleCallbacks[$event] ?? []; |
1161
|
|
|
} |
1162
|
|
|
|
1163
|
|
|
/** |
1164
|
|
|
* Adds a lifecycle callback for entities of this class. |
1165
|
|
|
*/ |
1166
|
16 |
|
public function addLifecycleCallback(string $eventName, string $methodName) |
1167
|
|
|
{ |
1168
|
16 |
|
if (in_array($methodName, $this->lifecycleCallbacks[$eventName] ?? [], true)) { |
1169
|
3 |
|
return; |
1170
|
|
|
} |
1171
|
|
|
|
1172
|
16 |
|
$this->lifecycleCallbacks[$eventName][] = $methodName; |
1173
|
16 |
|
} |
1174
|
|
|
|
1175
|
|
|
/** |
1176
|
|
|
* Adds a entity listener for entities of this class. |
1177
|
|
|
* |
1178
|
|
|
* @param string $eventName The entity lifecycle event. |
1179
|
|
|
* @param string $class The listener class. |
1180
|
|
|
* @param string $method The listener callback method. |
1181
|
|
|
* |
1182
|
|
|
* @throws MappingException |
1183
|
|
|
*/ |
1184
|
13 |
|
public function addEntityListener(string $eventName, string $class, string $methodName) : void |
1185
|
|
|
{ |
1186
|
|
|
$listener = [ |
1187
|
13 |
|
'class' => $class, |
1188
|
13 |
|
'method' => $methodName, |
1189
|
|
|
]; |
1190
|
|
|
|
1191
|
13 |
|
if (! class_exists($class)) { |
1192
|
1 |
|
throw MappingException::entityListenerClassNotFound($class, $this->className); |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
12 |
|
if (! method_exists($class, $methodName)) { |
1196
|
1 |
|
throw MappingException::entityListenerMethodNotFound($class, $methodName, $this->className); |
1197
|
|
|
} |
1198
|
|
|
|
1199
|
|
|
// Check if entity listener already got registered and ignore it if positive |
1200
|
11 |
|
if (in_array($listener, $this->entityListeners[$eventName] ?? [], true)) { |
1201
|
5 |
|
return; |
1202
|
|
|
} |
1203
|
|
|
|
1204
|
11 |
|
$this->entityListeners[$eventName][] = $listener; |
1205
|
11 |
|
} |
1206
|
|
|
|
1207
|
|
|
/** |
1208
|
|
|
* Sets the discriminator column definition. |
1209
|
|
|
* |
1210
|
|
|
* @see getDiscriminatorColumn() |
1211
|
|
|
* |
1212
|
|
|
* @throws MappingException |
1213
|
|
|
*/ |
1214
|
94 |
|
public function setDiscriminatorColumn(DiscriminatorColumnMetadata $discriminatorColumn) : void |
1215
|
|
|
{ |
1216
|
94 |
|
if (isset($this->fieldNames[$discriminatorColumn->getColumnName()])) { |
1217
|
|
|
throw MappingException::duplicateColumnName($this->className, $discriminatorColumn->getColumnName()); |
1218
|
|
|
} |
1219
|
|
|
|
1220
|
94 |
|
$discriminatorColumn->setTableName($discriminatorColumn->getTableName() ?? $this->getTableName()); |
1221
|
|
|
|
1222
|
94 |
|
$allowedTypeList = ['boolean', 'array', 'object', 'datetime', 'time', 'date']; |
1223
|
|
|
|
1224
|
94 |
|
if (in_array($discriminatorColumn->getTypeName(), $allowedTypeList, true)) { |
1225
|
|
|
throw MappingException::invalidDiscriminatorColumnType($discriminatorColumn->getTypeName()); |
1226
|
|
|
} |
1227
|
|
|
|
1228
|
94 |
|
$this->discriminatorColumn = $discriminatorColumn; |
1229
|
94 |
|
} |
1230
|
|
|
|
1231
|
|
|
/** |
1232
|
|
|
* Sets the discriminator values used by this class. |
1233
|
|
|
* Used for JOINED and SINGLE_TABLE inheritance mapping strategies. |
1234
|
|
|
* |
1235
|
|
|
* @param string[] $map |
1236
|
|
|
* |
1237
|
|
|
* @throws MappingException |
1238
|
|
|
*/ |
1239
|
89 |
|
public function setDiscriminatorMap(array $map) : void |
1240
|
|
|
{ |
1241
|
89 |
|
foreach ($map as $value => $className) { |
1242
|
89 |
|
$this->addDiscriminatorMapClass($value, $className); |
1243
|
|
|
} |
1244
|
89 |
|
} |
1245
|
|
|
|
1246
|
|
|
/** |
1247
|
|
|
* Adds one entry of the discriminator map with a new class and corresponding name. |
1248
|
|
|
* |
1249
|
|
|
* @param string|int $name |
1250
|
|
|
* |
1251
|
|
|
* @throws MappingException |
1252
|
|
|
*/ |
1253
|
89 |
|
public function addDiscriminatorMapClass($name, string $className) : void |
1254
|
|
|
{ |
1255
|
89 |
|
$this->discriminatorMap[$name] = $className; |
1256
|
|
|
|
1257
|
89 |
|
if ($this->className === $className) { |
1258
|
75 |
|
$this->discriminatorValue = $name; |
1259
|
|
|
|
1260
|
75 |
|
return; |
1261
|
|
|
} |
1262
|
|
|
|
1263
|
88 |
|
if (! (class_exists($className) || interface_exists($className))) { |
1264
|
|
|
throw MappingException::invalidClassInDiscriminatorMap($className, $this->className); |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
88 |
|
if (is_subclass_of($className, $this->className) && ! in_array($className, $this->subClasses, true)) { |
1268
|
83 |
|
$this->subClasses[] = $className; |
1269
|
|
|
} |
1270
|
88 |
|
} |
1271
|
|
|
|
1272
|
1031 |
|
public function getValueGenerationPlan() : ValueGenerationPlan |
1273
|
|
|
{ |
1274
|
1031 |
|
return $this->valueGenerationPlan; |
1275
|
|
|
} |
1276
|
|
|
|
1277
|
369 |
|
public function setValueGenerationPlan(ValueGenerationPlan $valueGenerationPlan) : void |
1278
|
|
|
{ |
1279
|
369 |
|
$this->valueGenerationPlan = $valueGenerationPlan; |
1280
|
369 |
|
} |
1281
|
|
|
|
1282
|
399 |
|
public function checkPropertyDuplication(string $columnName) : bool |
1283
|
|
|
{ |
1284
|
399 |
|
return isset($this->fieldNames[$columnName]) |
1285
|
399 |
|
|| ($this->discriminatorColumn !== null && $this->discriminatorColumn->getColumnName() === $columnName); |
1286
|
|
|
} |
1287
|
|
|
|
1288
|
|
|
/** |
1289
|
|
|
* Marks this class as read only, no change tracking is applied to it. |
1290
|
|
|
*/ |
1291
|
2 |
|
public function asReadOnly() : void |
1292
|
|
|
{ |
1293
|
2 |
|
$this->readOnly = true; |
1294
|
2 |
|
} |
1295
|
|
|
|
1296
|
|
|
/** |
1297
|
|
|
* Whether this class is read only or not. |
1298
|
|
|
*/ |
1299
|
446 |
|
public function isReadOnly() : bool |
1300
|
|
|
{ |
1301
|
446 |
|
return $this->readOnly; |
1302
|
|
|
} |
1303
|
|
|
|
1304
|
1091 |
|
public function isVersioned() : bool |
1305
|
|
|
{ |
1306
|
1091 |
|
return $this->versionProperty !== null; |
1307
|
|
|
} |
1308
|
|
|
|
1309
|
|
|
/** |
1310
|
|
|
* Map Embedded Class |
1311
|
|
|
* |
1312
|
|
|
* @param mixed[] $mapping |
1313
|
|
|
* |
1314
|
|
|
* @throws MappingException |
1315
|
|
|
*/ |
1316
|
|
|
public function mapEmbedded(array $mapping) : void |
|
|
|
|
1317
|
|
|
{ |
1318
|
|
|
/*if (isset($this->properties[$mapping['fieldName']])) { |
1319
|
|
|
throw MappingException::duplicateProperty($this->className, $this->getProperty($mapping['fieldName'])); |
1320
|
|
|
} |
1321
|
|
|
|
1322
|
|
|
$this->embeddedClasses[$mapping['fieldName']] = [ |
1323
|
|
|
'class' => $this->fullyQualifiedClassName($mapping['class']), |
1324
|
|
|
'columnPrefix' => $mapping['columnPrefix'], |
1325
|
|
|
'declaredField' => $mapping['declaredField'] ?? null, |
1326
|
|
|
'originalField' => $mapping['originalField'] ?? null, |
1327
|
|
|
'declaringClass' => $this, |
1328
|
|
|
];*/ |
1329
|
|
|
} |
1330
|
|
|
|
1331
|
|
|
/** |
1332
|
|
|
* Inline the embeddable class |
1333
|
|
|
* |
1334
|
|
|
* @param string $property |
1335
|
|
|
*/ |
1336
|
|
|
public function inlineEmbeddable($property, ClassMetadata $embeddable) : void |
|
|
|
|
1337
|
|
|
{ |
1338
|
|
|
/*foreach ($embeddable->fieldMappings as $fieldName => $fieldMapping) { |
1339
|
|
|
$fieldMapping['fieldName'] = $property . "." . $fieldName; |
1340
|
|
|
$fieldMapping['originalClass'] = $fieldMapping['originalClass'] ?? $embeddable->getClassName(); |
1341
|
|
|
$fieldMapping['originalField'] = $fieldMapping['originalField'] ?? $fieldName; |
1342
|
|
|
$fieldMapping['declaredField'] = isset($fieldMapping['declaredField']) |
1343
|
|
|
? $property . '.' . $fieldMapping['declaredField'] |
1344
|
|
|
: $property; |
1345
|
|
|
|
1346
|
|
|
if (! empty($this->embeddedClasses[$property]['columnPrefix'])) { |
1347
|
|
|
$fieldMapping['columnName'] = $this->embeddedClasses[$property]['columnPrefix'] . $fieldMapping['columnName']; |
1348
|
|
|
} elseif ($this->embeddedClasses[$property]['columnPrefix'] !== false) { |
1349
|
|
|
$fieldMapping['columnName'] = $this->namingStrategy->embeddedFieldToColumnName( |
1350
|
|
|
$property, |
1351
|
|
|
$fieldMapping['columnName'], |
1352
|
|
|
$this->reflectionClass->getName(), |
1353
|
|
|
$embeddable->reflectionClass->getName() |
1354
|
|
|
); |
1355
|
|
|
} |
1356
|
|
|
|
1357
|
|
|
$this->mapField($fieldMapping); |
1358
|
|
|
}*/ |
1359
|
|
|
} |
1360
|
|
|
} |
1361
|
|
|
|