-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ClassMetadata.php
1090 lines (923 loc) · 32.6 KB
/
ClassMetadata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Doctrine\ORM\Mapping;
use ArrayIterator;
use Doctrine\ORM\Cache\Exception\CacheException;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Reflection\ReflectionService;
use Doctrine\ORM\Sequencing\Planning\ValueGenerationPlan;
use Doctrine\ORM\Utility\PersisterHelper;
use ReflectionException;
use RuntimeException;
use function array_filter;
use function array_merge;
use function class_exists;
use function get_class;
use function in_array;
use function interface_exists;
use function is_subclass_of;
use function method_exists;
use function spl_object_id;
/**
* A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
* of an entity and its associations.
*/
class ClassMetadata extends ComponentMetadata implements TableOwner
{
/**
* The name of the custom repository class used for the entity class.
* (Optional).
*
* @var string
*/
protected $customRepositoryClassName;
/**
* READ-ONLY: Whether this class describes the mapping of a mapped superclass.
*
* @var bool
*/
public $isMappedSuperclass = false;
/**
* READ-ONLY: Whether this class describes the mapping of an embeddable class.
*
* @var bool
*/
public $isEmbeddedClass = false;
/**
* Whether this class describes the mapping of a read-only class.
* That means it is never considered for change-tracking in the UnitOfWork.
* It is a very helpful performance optimization for entities that are immutable,
* either in your domain or through the relation database (coming from a view,
* or a history table for example).
*
* @var bool
*/
private $readOnly = false;
/**
* The names of all subclasses (descendants).
*
* @var string[]
*/
protected $subClasses = [];
/**
* READ-ONLY: The names of all embedded classes based on properties.
*
* @var string[]
*/
//public $embeddedClasses = [];
/**
* READ-ONLY: The registered lifecycle callbacks for entities of this class.
*
* @var string[][]
*/
public $lifecycleCallbacks = [];
/**
* READ-ONLY: The registered entity listeners.
*
* @var mixed[][]
*/
public $entityListeners = [];
/**
* READ-ONLY: The field names of all fields that are part of the identifier/primary key
* of the mapped entity class.
*
* @var string[]
*/
public $identifier = [];
/**
* READ-ONLY: The inheritance mapping type used by the class.
*
* @var string
*/
public $inheritanceType = InheritanceType::NONE;
/**
* READ-ONLY: The policy used for change-tracking on entities of this class.
*
* @var string
*/
public $changeTrackingPolicy = ChangeTrackingPolicy::DEFERRED_IMPLICIT;
/**
* READ-ONLY: The discriminator value of this class.
*
* <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
* where a discriminator column is used.</b>
*
* @see discriminatorColumn
*
* @var mixed
*/
public $discriminatorValue;
/**
* READ-ONLY: The discriminator map of all mapped classes in the hierarchy.
*
* <b>This does only apply to the JOINED and SINGLE_TABLE inheritance mapping strategies
* where a discriminator column is used.</b>
*
* @see discriminatorColumn
*
* @var string[]
*/
public $discriminatorMap = [];
/**
* READ-ONLY: The definition of the discriminator column used in JOINED and SINGLE_TABLE
* inheritance mappings.
*
* @var DiscriminatorColumnMetadata
*/
public $discriminatorColumn;
/**
* READ-ONLY: The primary table metadata.
*
* @var TableMetadata
*/
public $table;
/**
* READ-ONLY: An array of field names. Used to look up field names from column names.
* Keys are column names and values are field names.
*
* @var string[]
*/
public $fieldNames = [];
/**
* READ-ONLY: The field which is used for versioning in optimistic locking (if any).
*
* @var FieldMetadata|null
*/
public $versionProperty;
/**
* Value generation plan is responsible for generating values for auto-generated fields.
*
* @var ValueGenerationPlan
*/
protected $valueGenerationPlan;
/**
* Initializes a new ClassMetadata instance that will hold the object-relational mapping
* metadata of the class with the given name.
*
* @param string $entityName The name of the entity class.
* @param ClassMetadata|null $parent Optional parent class metadata.
*/
public function __construct(string $entityName, ?ComponentMetadata $parent)
{
parent::__construct($entityName);
if ($parent) {
$this->setParent($parent);
}
}
/**
* {@inheritdoc}
*
* @throws MappingException
*/
public function setParent(ComponentMetadata $parent) : void
{
parent::setParent($parent);
foreach ($parent->getPropertiesIterator() as $fieldName => $property) {
$this->addInheritedProperty($property);
}
// @todo guilhermeblanco Assume to be a ClassMetadata temporarily until ClassMetadata split is complete.
/** @var ClassMetadata $parent */
$this->setInheritanceType($parent->inheritanceType);
$this->setIdentifier($parent->identifier);
$this->setChangeTrackingPolicy($parent->changeTrackingPolicy);
if ($parent->discriminatorColumn) {
$this->setDiscriminatorColumn($parent->discriminatorColumn);
$this->setDiscriminatorMap($parent->discriminatorMap);
}
if ($parent->isMappedSuperclass) {
$this->setCustomRepositoryClassName($parent->getCustomRepositoryClassName());
}
if ($parent->cache) {
$this->setCache(clone $parent->cache);
}
if (! empty($parent->lifecycleCallbacks)) {
$this->lifecycleCallbacks = $parent->lifecycleCallbacks;
}
if (! empty($parent->entityListeners)) {
$this->entityListeners = $parent->entityListeners;
}
}
public function setClassName(string $className)
{
$this->className = $className;
}
public function getColumnsIterator() : ArrayIterator
{
$iterator = parent::getColumnsIterator();
if ($this->discriminatorColumn) {
$iterator->offsetSet($this->discriminatorColumn->getColumnName(), $this->discriminatorColumn);
}
return $iterator;
}
public function getAncestorsIterator() : ArrayIterator
{
$ancestors = new ArrayIterator();
$parent = $this;
while (($parent = $parent->parent) !== null) {
if ($parent instanceof ClassMetadata && $parent->isMappedSuperclass) {
continue;
}
$ancestors->append($parent);
}
return $ancestors;
}
public function getRootClassName() : string
{
return $this->parent instanceof ClassMetadata && ! $this->parent->isMappedSuperclass
? $this->parent->getRootClassName()
: $this->className;
}
/**
* Handles metadata cloning nicely.
*/
public function __clone()
{
if ($this->cache) {
$this->cache = clone $this->cache;
}
foreach ($this->properties as $name => $property) {
$this->properties[$name] = clone $property;
}
}
/**
* Creates a string representation of this instance.
*
* @return string The string representation of this instance.
*
* @todo Construct meaningful string representation.
*/
public function __toString()
{
return self::class . '@' . spl_object_id($this);
}
/**
* Determines which fields get serialized.
*
* It is only serialized what is necessary for best unserialization performance.
* That means any metadata properties that are not set or empty or simply have
* their default value are NOT serialized.
*
* Parts that are also NOT serialized because they can not be properly unserialized:
* - reflectionClass
*
* @return string[] The names of all the fields that should be serialized.
*/
public function __sleep()
{
$serialized = [];
// This metadata is always serialized/cached.
$serialized = array_merge($serialized, [
'properties',
'fieldNames',
//'embeddedClasses',
'identifier',
'className',
'parent',
'table',
'valueGenerationPlan',
]);
// The rest of the metadata is only serialized if necessary.
if ($this->changeTrackingPolicy !== ChangeTrackingPolicy::DEFERRED_IMPLICIT) {
$serialized[] = 'changeTrackingPolicy';
}
if ($this->customRepositoryClassName) {
$serialized[] = 'customRepositoryClassName';
}
if ($this->inheritanceType !== InheritanceType::NONE) {
$serialized[] = 'inheritanceType';
$serialized[] = 'discriminatorColumn';
$serialized[] = 'discriminatorValue';
$serialized[] = 'discriminatorMap';
$serialized[] = 'subClasses';
}
if ($this->isMappedSuperclass) {
$serialized[] = 'isMappedSuperclass';
}
if ($this->isEmbeddedClass) {
$serialized[] = 'isEmbeddedClass';
}
if ($this->isVersioned()) {
$serialized[] = 'versionProperty';
}
if ($this->lifecycleCallbacks) {
$serialized[] = 'lifecycleCallbacks';
}
if ($this->entityListeners) {
$serialized[] = 'entityListeners';
}
if ($this->cache) {
$serialized[] = 'cache';
}
if ($this->readOnly) {
$serialized[] = 'readOnly';
}
return $serialized;
}
/**
* Sets the change tracking policy used by this class.
*/
public function setChangeTrackingPolicy(string $policy) : void
{
$this->changeTrackingPolicy = $policy;
}
/**
* Checks whether a field is part of the identifier/primary key field(s).
*
* @param string $fieldName The field name.
*
* @return bool TRUE if the field is part of the table identifier/primary key field(s), FALSE otherwise.
*/
public function isIdentifier(string $fieldName) : bool
{
if (! $this->identifier) {
return false;
}
if (! $this->isIdentifierComposite()) {
return $fieldName === $this->identifier[0];
}
return in_array($fieldName, $this->identifier, true);
}
public function isIdentifierComposite() : bool
{
return isset($this->identifier[1]);
}
/**
* Validates Identifier.
*
* @throws MappingException
*/
public function validateIdentifier() : void
{
if ($this->isMappedSuperclass || $this->isEmbeddedClass) {
return;
}
// Verify & complete identifier mapping
if (! $this->identifier) {
throw MappingException::identifierRequired($this->className);
}
$explicitlyGeneratedProperties = array_filter($this->properties, static function (Property $property) : bool {
return $property instanceof FieldMetadata
&& $property->isPrimaryKey()
&& $property->hasValueGenerator();
});
if ($explicitlyGeneratedProperties && $this->isIdentifierComposite()) {
throw MappingException::compositeKeyAssignedIdGeneratorRequired($this->className);
}
}
/**
* Validates lifecycle callbacks.
*
* @throws MappingException
*/
public function validateLifecycleCallbacks(ReflectionService $reflectionService) : void
{
foreach ($this->lifecycleCallbacks as $callbacks) {
/** @var array $callbacks */
foreach ($callbacks as $callbackFuncName) {
if (! $reflectionService->hasPublicMethod($this->className, $callbackFuncName)) {
throw MappingException::lifecycleCallbackMethodNotFound($this->className, $callbackFuncName);
}
}
}
}
/**
* {@inheritDoc}
*/
public function getIdentifierFieldNames()
{
return $this->identifier;
}
/**
* Gets the name of the single id field. Note that this only works on
* entity classes that have a single-field pk.
*
* @return string
*
* @throws MappingException If the class has a composite primary key.
*/
public function getSingleIdentifierFieldName()
{
if ($this->isIdentifierComposite()) {
throw MappingException::singleIdNotAllowedOnCompositePrimaryKey($this->className);
}
if (! isset($this->identifier[0])) {
throw MappingException::noIdDefined($this->className);
}
return $this->identifier[0];
}
/**
* INTERNAL:
* Sets the mapped identifier/primary key fields of this class.
* Mainly used by the ClassMetadataFactory to assign inherited identifiers.
*
* @param mixed[] $identifier
*/
public function setIdentifier(array $identifier)
{
$this->identifier = $identifier;
}
/**
* {@inheritDoc}
*/
public function getIdentifier()
{
return $this->identifier;
}
/**
* {@inheritDoc}
*/
public function hasField($fieldName)
{
return isset($this->properties[$fieldName])
&& $this->properties[$fieldName] instanceof FieldMetadata;
}
/**
* Returns an array with identifier column names and their corresponding ColumnMetadata.
*
* @return ColumnMetadata[]
*/
public function getIdentifierColumns(EntityManagerInterface $em) : array
{
$columns = [];
foreach ($this->identifier as $idProperty) {
$property = $this->getProperty($idProperty);
if ($property instanceof FieldMetadata) {
$columns[$property->getColumnName()] = $property;
continue;
}
/** @var AssociationMetadata $property */
// Association defined as Id field
$targetClass = $em->getClassMetadata($property->getTargetEntity());
if (! $property->isOwningSide()) {
$property = $targetClass->getProperty($property->getMappedBy());
$targetClass = $em->getClassMetadata($property->getTargetEntity());
}
$joinColumns = $property instanceof ManyToManyAssociationMetadata
? $property->getJoinTable()->getInverseJoinColumns()
: $property->getJoinColumns();
foreach ($joinColumns as $joinColumn) {
/** @var JoinColumnMetadata $joinColumn */
$columnName = $joinColumn->getColumnName();
$referencedColumnName = $joinColumn->getReferencedColumnName();
if (! $joinColumn->getType()) {
$joinColumn->setType(PersisterHelper::getTypeOfColumn($referencedColumnName, $targetClass, $em));
}
$columns[$columnName] = $joinColumn;
}
}
return $columns;
}
/**
* Gets the name of the primary table.
*/
public function getTableName() : ?string
{
return $this->table->getName();
}
/**
* Gets primary table's schema name.
*/
public function getSchemaName() : ?string
{
return $this->table->getSchema();
}
/**
* Gets the table name to use for temporary identifier tables of this class.
*/
public function getTemporaryIdTableName() : string
{
$schema = $this->getSchemaName() === null
? ''
: $this->getSchemaName() . '_';
// replace dots with underscores because PostgreSQL creates temporary tables in a special schema
return $schema . $this->getTableName() . '_id_tmp';
}
/**
* Sets the mapped subclasses of this class.
*
* @param string[] $subclasses The names of all mapped subclasses.
*
* @todo guilhermeblanco Only used for ClassMetadataTest. Remove if possible!
*/
public function setSubclasses(array $subclasses) : void
{
foreach ($subclasses as $subclass) {
$this->subClasses[] = $subclass;
}
}
/**
* @return string[]
*/
public function getSubClasses() : array
{
return $this->subClasses;
}
/**
* Sets the inheritance type used by the class and its subclasses.
*
* @param int $type
*
* @throws MappingException
*/
public function setInheritanceType($type) : void
{
if (! $this->isInheritanceType($type)) {
throw MappingException::invalidInheritanceType($this->className, $type);
}
$this->inheritanceType = $type;
}
/**
* Sets the override property mapping for an entity relationship.
*
* @throws RuntimeException
* @throws MappingException
* @throws CacheException
*/
public function setPropertyOverride(Property $property) : void
{
$fieldName = $property->getName();
if (! isset($this->properties[$fieldName])) {
throw MappingException::invalidOverrideFieldName($this->className, $fieldName);
}
$originalProperty = $this->getProperty($fieldName);
$originalPropertyClassName = get_class($originalProperty);
// If moving from transient to persistent, assume it's a new property
if ($originalPropertyClassName === TransientMetadata::class) {
unset($this->properties[$fieldName]);
$this->addProperty($property);
return;
}
// Do not allow to change property type
if ($originalPropertyClassName !== get_class($property)) {
throw MappingException::invalidOverridePropertyType($this->className, $fieldName);
}
// Do not allow to change version property
if ($originalProperty instanceof FieldMetadata && $originalProperty->isVersioned()) {
throw MappingException::invalidOverrideVersionField($this->className, $fieldName);
}
unset($this->properties[$fieldName]);
if ($property instanceof FieldMetadata) {
// Unset defined fieldName prior to override
unset($this->fieldNames[$originalProperty->getColumnName()]);
// Revert what should not be allowed to change
$property->setDeclaringClass($originalProperty->getDeclaringClass());
$property->setPrimaryKey($originalProperty->isPrimaryKey());
} elseif ($property instanceof AssociationMetadata) {
// Unset all defined fieldNames prior to override
if ($originalProperty instanceof ToOneAssociationMetadata && $originalProperty->isOwningSide()) {
foreach ($originalProperty->getJoinColumns() as $joinColumn) {
unset($this->fieldNames[$joinColumn->getColumnName()]);
}
}
// Override what it should be allowed to change
if ($property->getInversedBy()) {
$originalProperty->setInversedBy($property->getInversedBy());
}
if ($property->getFetchMode() !== $originalProperty->getFetchMode()) {
$originalProperty->setFetchMode($property->getFetchMode());
}
if ($originalProperty instanceof ToOneAssociationMetadata && $property->getJoinColumns()) {
$originalProperty->setJoinColumns($property->getJoinColumns());
} elseif ($originalProperty instanceof ManyToManyAssociationMetadata && $property->getJoinTable()) {
$originalProperty->setJoinTable($property->getJoinTable());
}
$property = $originalProperty;
}
$this->addProperty($property);
}
/**
* Checks if this entity is the root in any entity-inheritance-hierarchy.
*
* @return bool
*/
public function isRootEntity()
{
return $this->className === $this->getRootClassName();
}
/**
* Checks whether a mapped field is inherited from a superclass.
*
* @param string $fieldName
*
* @return bool TRUE if the field is inherited, FALSE otherwise.
*/
public function isInheritedProperty($fieldName)
{
$declaringClass = $this->properties[$fieldName]->getDeclaringClass();
return $declaringClass->className !== $this->className;
}
/**
* {@inheritdoc}
*/
public function setTable(TableMetadata $table) : void
{
$this->table = $table;
// Make sure inherited and declared properties reflect newly defined table
foreach ($this->properties as $property) {
switch (true) {
case $property instanceof FieldMetadata:
$property->setTableName($property->getTableName() ?? $table->getName());
break;
case $property instanceof ToOneAssociationMetadata:
// Resolve association join column table names
foreach ($property->getJoinColumns() as $joinColumn) {
/** @var JoinColumnMetadata $joinColumn */
$joinColumn->setTableName($joinColumn->getTableName() ?? $table->getName());
}
break;
}
}
}
/**
* Checks whether the given type identifies an inheritance type.
*
* @param int $type
*
* @return bool TRUE if the given type identifies an inheritance type, FALSe otherwise.
*/
private function isInheritanceType($type)
{
return $type === InheritanceType::NONE
|| $type === InheritanceType::SINGLE_TABLE
|| $type === InheritanceType::JOINED
|| $type === InheritanceType::TABLE_PER_CLASS;
}
public function getColumn(string $columnName) : ?LocalColumnMetadata
{
foreach ($this->properties as $property) {
if ($property instanceof LocalColumnMetadata && $property->getColumnName() === $columnName) {
return $property;
}
}
return null;
}
/**
* Add a property mapping.
*
* @throws RuntimeException
* @throws MappingException
* @throws CacheException
* @throws ReflectionException
*/
public function addProperty(Property $property) : void
{
$fieldName = $property->getName();
// Check for empty field name
if (empty($fieldName)) {
throw MappingException::missingFieldName($this->className);
}
$property->setDeclaringClass($this);
switch (true) {
case $property instanceof FieldMetadata:
if ($property->isVersioned()) {
$this->versionProperty = $property;
}
break;
default:
// Transient properties are ignored on purpose here! =)
break;
}
if ($property->isPrimaryKey() && ! in_array($fieldName, $this->identifier, true)) {
$this->identifier[] = $fieldName;
}
parent::addProperty($property);
}
/**
* INTERNAL:
* Adds a property mapping without completing/validating it.
* This is mainly used to add inherited property mappings to derived classes.
*/
public function addInheritedProperty(Property $property)
{
if (isset($this->properties[$property->getName()])) {
throw MappingException::duplicateProperty($this->className, $this->getProperty($property->getName()));
}
$declaringClass = $property->getDeclaringClass();
$inheritedProperty = $declaringClass->isMappedSuperclass ? clone $property : $property;
if ($inheritedProperty instanceof FieldMetadata) {
if (! $declaringClass->isMappedSuperclass) {
$inheritedProperty->setTableName($property->getTableName());
}
if ($inheritedProperty->isVersioned()) {
$this->versionProperty = $inheritedProperty;
}
$this->fieldNames[$property->getColumnName()] = $property->getName();
} elseif ($inheritedProperty instanceof AssociationMetadata) {
if ($declaringClass->isMappedSuperclass) {
$inheritedProperty->setSourceEntity($this->className);
}
// Need to add inherited fieldNames
if ($inheritedProperty instanceof ToOneAssociationMetadata && $inheritedProperty->isOwningSide()) {
foreach ($inheritedProperty->getJoinColumns() as $joinColumn) {
/** @var JoinColumnMetadata $joinColumn */
$this->fieldNames[$joinColumn->getColumnName()] = $property->getName();
}
}
}
$this->properties[$property->getName()] = $inheritedProperty;
}
/**
* Registers a custom repository class for the entity class.
*
* @param string|null $repositoryClassName The class name of the custom mapper.
*/
public function setCustomRepositoryClassName(?string $repositoryClassName)
{
$this->customRepositoryClassName = $repositoryClassName;
}
public function getCustomRepositoryClassName() : ?string
{
return $this->customRepositoryClassName;
}
/**
* Whether the class has any attached lifecycle listeners or callbacks for a lifecycle event.
*
* @param string $lifecycleEvent
*
* @return bool
*/
public function hasLifecycleCallbacks($lifecycleEvent)
{
return isset($this->lifecycleCallbacks[$lifecycleEvent]);
}
/**
* Gets the registered lifecycle callbacks for an event.
*
* @param string $event
*
* @return string[]
*/
public function getLifecycleCallbacks($event) : array
{
return $this->lifecycleCallbacks[$event] ?? [];
}
/**
* Adds a lifecycle callback for entities of this class.
*/
public function addLifecycleCallback(string $eventName, string $methodName)
{
if (in_array($methodName, $this->lifecycleCallbacks[$eventName] ?? [], true)) {
return;
}
$this->lifecycleCallbacks[$eventName][] = $methodName;
}
/**
* Adds a entity listener for entities of this class.
*
* @param string $eventName The entity lifecycle event.
* @param string $class The listener class.
* @param string $method The listener callback method.
*
* @throws MappingException
*/
public function addEntityListener(string $eventName, string $class, string $methodName) : void
{
$listener = [
'class' => $class,
'method' => $methodName,
];
if (! class_exists($class)) {
throw MappingException::entityListenerClassNotFound($class, $this->className);
}
if (! method_exists($class, $methodName)) {
throw MappingException::entityListenerMethodNotFound($class, $methodName, $this->className);
}
// Check if entity listener already got registered and ignore it if positive
if (in_array($listener, $this->entityListeners[$eventName] ?? [], true)) {
return;
}
$this->entityListeners[$eventName][] = $listener;
}
/**
* Sets the discriminator column definition.
*
* @see getDiscriminatorColumn()
*
* @throws MappingException
*/
public function setDiscriminatorColumn(DiscriminatorColumnMetadata $discriminatorColumn) : void
{
if (isset($this->fieldNames[$discriminatorColumn->getColumnName()])) {
throw MappingException::duplicateColumnName($this->className, $discriminatorColumn->getColumnName());
}
$discriminatorColumn->setTableName($discriminatorColumn->getTableName() ?? $this->getTableName());
$allowedTypeList = ['boolean', 'array', 'object', 'datetime', 'time', 'date'];
if (in_array($discriminatorColumn->getTypeName(), $allowedTypeList, true)) {
throw MappingException::invalidDiscriminatorColumnType($discriminatorColumn->getTypeName());
}
$this->discriminatorColumn = $discriminatorColumn;
}
/**
* Sets the discriminator values used by this class.
* Used for JOINED and SINGLE_TABLE inheritance mapping strategies.
*
* @param string[] $map
*
* @throws MappingException
*/
public function setDiscriminatorMap(array $map) : void
{
foreach ($map as $value => $className) {
$this->addDiscriminatorMapClass($value, $className);
}
}
/**
* Adds one entry of the discriminator map with a new class and corresponding name.
*
* @param string|int $name
*
* @throws MappingException
*/
public function addDiscriminatorMapClass($name, string $className) : void
{
$this->discriminatorMap[$name] = $className;
if ($this->className === $className) {
$this->discriminatorValue = $name;
return;
}
if (! (class_exists($className) || interface_exists($className))) {
throw MappingException::invalidClassInDiscriminatorMap($className, $this->className);
}
if (is_subclass_of($className, $this->className) && ! in_array($className, $this->subClasses, true)) {
$this->subClasses[] = $className;
}
}