vendor/doctrine/orm/src/Mapping/ClassMetadataFactory.php line 267

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Mapping;
  4. use Doctrine\Common\EventManager;
  5. use Doctrine\DBAL\Platforms;
  6. use Doctrine\DBAL\Platforms\AbstractPlatform;
  7. use Doctrine\Deprecations\Deprecation;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
  10. use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs;
  11. use Doctrine\ORM\Events;
  12. use Doctrine\ORM\Exception\ORMException;
  13. use Doctrine\ORM\Id\AssignedGenerator;
  14. use Doctrine\ORM\Id\BigIntegerIdentityGenerator;
  15. use Doctrine\ORM\Id\IdentityGenerator;
  16. use Doctrine\ORM\Id\SequenceGenerator;
  17. use Doctrine\ORM\Mapping\Exception\InvalidCustomGenerator;
  18. use Doctrine\ORM\Mapping\Exception\UnknownGeneratorType;
  19. use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
  20. use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
  21. use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
  22. use Doctrine\Persistence\Mapping\Driver\MappingDriver;
  23. use Doctrine\Persistence\Mapping\ReflectionService;
  24. use ReflectionClass;
  25. use ReflectionException;
  26. use function assert;
  27. use function class_exists;
  28. use function count;
  29. use function end;
  30. use function explode;
  31. use function in_array;
  32. use function is_a;
  33. use function is_subclass_of;
  34. use function method_exists;
  35. use function str_contains;
  36. use function strlen;
  37. use function strtolower;
  38. use function substr;
  39. /**
  40.  * The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
  41.  * metadata mapping information of a class which describes how a class should be mapped
  42.  * to a relational database.
  43.  *
  44.  * @extends AbstractClassMetadataFactory<ClassMetadata>
  45.  */
  46. class ClassMetadataFactory extends AbstractClassMetadataFactory
  47. {
  48.     private EntityManagerInterface|null $em       null;
  49.     private AbstractPlatform|null $targetPlatform null;
  50.     private MappingDriver|null $driver            null;
  51.     private EventManager|null $evm                null;
  52.     /** @var mixed[] */
  53.     private array $embeddablesActiveNesting = [];
  54.     private const NON_IDENTITY_DEFAULT_STRATEGY = [
  55.         Platforms\OraclePlatform::class => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
  56.     ];
  57.     public function setEntityManager(EntityManagerInterface $em): void
  58.     {
  59.         parent::setProxyClassNameResolver(new DefaultProxyClassNameResolver());
  60.         $this->em $em;
  61.     }
  62.     /**
  63.      * @param A $maybeOwningSide
  64.      *
  65.      * @return (A is ManyToManyAssociationMapping ? ManyToManyOwningSideMapping : (
  66.      *     A is OneToOneAssociationMapping ? OneToOneOwningSideMapping : (
  67.      *     A is OneToManyAssociationMapping ? ManyToOneAssociationMapping : (
  68.      *     A is ManyToOneAssociationMapping ? ManyToOneAssociationMapping :
  69.      *     ManyToManyOwningSideMapping|OneToOneOwningSideMapping|ManyToOneAssociationMapping
  70.      * ))))
  71.      *
  72.      * @template A of AssociationMapping
  73.      */
  74.     final public function getOwningSide(AssociationMapping $maybeOwningSide): OwningSideMapping
  75.     {
  76.         if ($maybeOwningSide instanceof OwningSideMapping) {
  77.             assert($maybeOwningSide instanceof ManyToManyOwningSideMapping ||
  78.                 $maybeOwningSide instanceof OneToOneOwningSideMapping ||
  79.                 $maybeOwningSide instanceof ManyToOneAssociationMapping);
  80.             return $maybeOwningSide;
  81.         }
  82.         assert($maybeOwningSide instanceof InverseSideMapping);
  83.         $owningSide $this->getMetadataFor($maybeOwningSide->targetEntity)
  84.             ->associationMappings[$maybeOwningSide->mappedBy];
  85.         assert($owningSide instanceof ManyToManyOwningSideMapping ||
  86.             $owningSide instanceof OneToOneOwningSideMapping ||
  87.             $owningSide instanceof ManyToOneAssociationMapping);
  88.         return $owningSide;
  89.     }
  90.     protected function initialize(): void
  91.     {
  92.         $this->driver      $this->em->getConfiguration()->getMetadataDriverImpl();
  93.         $this->evm         $this->em->getEventManager();
  94.         $this->initialized true;
  95.     }
  96.     protected function onNotFoundMetadata(string $className): ClassMetadata|null
  97.     {
  98.         if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
  99.             return null;
  100.         }
  101.         $eventArgs = new OnClassMetadataNotFoundEventArgs($className$this->em);
  102.         $this->evm->dispatchEvent(Events::onClassMetadataNotFound$eventArgs);
  103.         $classMetadata $eventArgs->getFoundMetadata();
  104.         assert($classMetadata instanceof ClassMetadata || $classMetadata === null);
  105.         return $classMetadata;
  106.     }
  107.     /**
  108.      * {@inheritDoc}
  109.      */
  110.     protected function doLoadMetadata(
  111.         ClassMetadataInterface $class,
  112.         ClassMetadataInterface|null $parent,
  113.         bool $rootEntityFound,
  114.         array $nonSuperclassParents,
  115.     ): void {
  116.         if ($parent) {
  117.             $class->setInheritanceType($parent->inheritanceType);
  118.             $class->setDiscriminatorColumn($parent->discriminatorColumn === null null : clone $parent->discriminatorColumn);
  119.             $class->setIdGeneratorType($parent->generatorType);
  120.             $this->addInheritedFields($class$parent);
  121.             $this->addInheritedRelations($class$parent);
  122.             $this->addInheritedEmbeddedClasses($class$parent);
  123.             $class->setIdentifier($parent->identifier);
  124.             $class->setVersioned($parent->isVersioned);
  125.             $class->setVersionField($parent->versionField);
  126.             $class->setDiscriminatorMap($parent->discriminatorMap);
  127.             $class->addSubClasses($parent->subClasses);
  128.             $class->setLifecycleCallbacks($parent->lifecycleCallbacks);
  129.             $class->setChangeTrackingPolicy($parent->changeTrackingPolicy);
  130.             if (! empty($parent->customGeneratorDefinition)) {
  131.                 $class->setCustomGeneratorDefinition($parent->customGeneratorDefinition);
  132.             }
  133.             if ($parent->isMappedSuperclass) {
  134.                 $class->setCustomRepositoryClass($parent->customRepositoryClassName);
  135.             }
  136.         }
  137.         // Invoke driver
  138.         try {
  139.             $this->driver->loadMetadataForClass($class->getName(), $class);
  140.         } catch (ReflectionException $e) {
  141.             throw MappingException::reflectionFailure($class->getName(), $e);
  142.         }
  143.         // If this class has a parent the id generator strategy is inherited.
  144.         // However this is only true if the hierarchy of parents contains the root entity,
  145.         // if it consists of mapped superclasses these don't necessarily include the id field.
  146.         if ($parent && $rootEntityFound) {
  147.             $this->inheritIdGeneratorMapping($class$parent);
  148.         } else {
  149.             $this->completeIdGeneratorMapping($class);
  150.         }
  151.         if (! $class->isMappedSuperclass) {
  152.             if ($rootEntityFound && $class->isInheritanceTypeNone()) {
  153.                 throw MappingException::missingInheritanceTypeDeclaration(end($nonSuperclassParents), $class->name);
  154.             }
  155.             foreach ($class->embeddedClasses as $property => $embeddableClass) {
  156.                 if (isset($embeddableClass->inherited)) {
  157.                     continue;
  158.                 }
  159.                 if (isset($this->embeddablesActiveNesting[$embeddableClass->class])) {
  160.                     throw MappingException::infiniteEmbeddableNesting($class->name$property);
  161.                 }
  162.                 $this->embeddablesActiveNesting[$class->name] = true;
  163.                 $embeddableMetadata $this->getMetadataFor($embeddableClass->class);
  164.                 if ($embeddableMetadata->isEmbeddedClass) {
  165.                     $this->addNestedEmbeddedClasses($embeddableMetadata$class$property);
  166.                 }
  167.                 $identifier $embeddableMetadata->getIdentifier();
  168.                 if (! empty($identifier)) {
  169.                     $this->inheritIdGeneratorMapping($class$embeddableMetadata);
  170.                 }
  171.                 $class->inlineEmbeddable($property$embeddableMetadata);
  172.                 unset($this->embeddablesActiveNesting[$class->name]);
  173.             }
  174.         }
  175.         if ($parent) {
  176.             if ($parent->isInheritanceTypeSingleTable()) {
  177.                 $class->setPrimaryTable($parent->table);
  178.             }
  179.             $this->addInheritedIndexes($class$parent);
  180.             if ($parent->cache) {
  181.                 $class->cache $parent->cache;
  182.             }
  183.             if ($parent->containsForeignIdentifier) {
  184.                 $class->containsForeignIdentifier true;
  185.             }
  186.             if ($parent->containsEnumIdentifier) {
  187.                 $class->containsEnumIdentifier true;
  188.             }
  189.             if (! empty($parent->entityListeners) && empty($class->entityListeners)) {
  190.                 $class->entityListeners $parent->entityListeners;
  191.             }
  192.         }
  193.         $class->setParentClasses($nonSuperclassParents);
  194.         if ($class->isRootEntity() && ! $class->isInheritanceTypeNone() && ! $class->discriminatorMap) {
  195.             $this->addDefaultDiscriminatorMap($class);
  196.         }
  197.         // During the following event, there may also be updates to the discriminator map as per GH-1257/GH-8402.
  198.         // So, we must not discover the missing subclasses before that.
  199.         if ($this->evm->hasListeners(Events::loadClassMetadata)) {
  200.             $eventArgs = new LoadClassMetadataEventArgs($class$this->em);
  201.             $this->evm->dispatchEvent(Events::loadClassMetadata$eventArgs);
  202.         }
  203.         $this->findAbstractEntityClassesNotListedInDiscriminatorMap($class);
  204.         $this->validateRuntimeMetadata($class$parent);
  205.     }
  206.     /**
  207.      * Validate runtime metadata is correctly defined.
  208.      *
  209.      * @throws MappingException
  210.      */
  211.     protected function validateRuntimeMetadata(ClassMetadata $classClassMetadataInterface|null $parent): void
  212.     {
  213.         if (! $class->reflClass) {
  214.             // only validate if there is a reflection class instance
  215.             return;
  216.         }
  217.         $class->validateIdentifier();
  218.         $class->validateAssociations();
  219.         $class->validateLifecycleCallbacks($this->getReflectionService());
  220.         // verify inheritance
  221.         if (! $class->isMappedSuperclass && ! $class->isInheritanceTypeNone()) {
  222.             if (! $parent) {
  223.                 if (count($class->discriminatorMap) === 0) {
  224.                     throw MappingException::missingDiscriminatorMap($class->name);
  225.                 }
  226.                 if (! $class->discriminatorColumn) {
  227.                     throw MappingException::missingDiscriminatorColumn($class->name);
  228.                 }
  229.                 foreach ($class->subClasses as $subClass) {
  230.                     if ((new ReflectionClass($subClass))->name !== $subClass) {
  231.                         throw MappingException::invalidClassInDiscriminatorMap($subClass$class->name);
  232.                     }
  233.                 }
  234.             } else {
  235.                 assert($parent instanceof ClassMetadata); // https://github.com/doctrine/orm/issues/8746
  236.                 if (
  237.                     ! $class->reflClass->isAbstract()
  238.                     && ! in_array($class->name$class->discriminatorMaptrue)
  239.                 ) {
  240.                     throw MappingException::mappedClassNotPartOfDiscriminatorMap($class->name$class->rootEntityName);
  241.                 }
  242.             }
  243.         } elseif ($class->isMappedSuperclass && $class->name === $class->rootEntityName && (count($class->discriminatorMap) || $class->discriminatorColumn)) {
  244.             // second condition is necessary for mapped superclasses in the middle of an inheritance hierarchy
  245.             throw MappingException::noInheritanceOnMappedSuperClass($class->name);
  246.         }
  247.     }
  248.     protected function newClassMetadataInstance(string $className): ClassMetadata
  249.     {
  250.         return new ClassMetadata(
  251.             $className,
  252.             $this->em->getConfiguration()->getNamingStrategy(),
  253.             $this->em->getConfiguration()->getTypedFieldMapper(),
  254.         );
  255.     }
  256.     /**
  257.      * Adds a default discriminator map if no one is given
  258.      *
  259.      * If an entity is of any inheritance type and does not contain a
  260.      * discriminator map, then the map is generated automatically. This process
  261.      * is expensive computation wise.
  262.      *
  263.      * The automatically generated discriminator map contains the lowercase short name of
  264.      * each class as key.
  265.      *
  266.      * @throws MappingException
  267.      */
  268.     private function addDefaultDiscriminatorMap(ClassMetadata $class): void
  269.     {
  270.         $allClasses $this->driver->getAllClassNames();
  271.         $fqcn       $class->getName();
  272.         $map        = [$this->getShortName($class->name) => $fqcn];
  273.         $duplicates = [];
  274.         foreach ($allClasses as $subClassCandidate) {
  275.             if (is_subclass_of($subClassCandidate$fqcn)) {
  276.                 $shortName $this->getShortName($subClassCandidate);
  277.                 if (isset($map[$shortName])) {
  278.                     $duplicates[] = $shortName;
  279.                 }
  280.                 $map[$shortName] = $subClassCandidate;
  281.             }
  282.         }
  283.         if ($duplicates) {
  284.             throw MappingException::duplicateDiscriminatorEntry($class->name$duplicates$map);
  285.         }
  286.         $class->setDiscriminatorMap($map);
  287.     }
  288.     private function findAbstractEntityClassesNotListedInDiscriminatorMap(ClassMetadata $rootEntityClass): void
  289.     {
  290.         // Only root classes in inheritance hierarchies need contain a discriminator map,
  291.         // so skip for other classes.
  292.         if (! $rootEntityClass->isRootEntity() || $rootEntityClass->isInheritanceTypeNone()) {
  293.             return;
  294.         }
  295.         $processedClasses = [$rootEntityClass->name => true];
  296.         foreach ($rootEntityClass->subClasses as $knownSubClass) {
  297.             $processedClasses[$knownSubClass] = true;
  298.         }
  299.         foreach ($rootEntityClass->discriminatorMap as $declaredClassName) {
  300.             // This fetches non-transient parent classes only
  301.             $parentClasses $this->getParentClasses($declaredClassName);
  302.             foreach ($parentClasses as $parentClass) {
  303.                 if (isset($processedClasses[$parentClass])) {
  304.                     continue;
  305.                 }
  306.                 $processedClasses[$parentClass] = true;
  307.                 // All non-abstract entity classes must be listed in the discriminator map, and
  308.                 // this will be validated/enforced at runtime (possibly at a later time, when the
  309.                 // subclass is loaded, but anyways). Also, subclasses is about entity classes only.
  310.                 // That means we can ignore non-abstract classes here. The (expensive) driver
  311.                 // check for mapped superclasses need only be run for abstract candidate classes.
  312.                 if (! (new ReflectionClass($parentClass))->isAbstract() || $this->peekIfIsMappedSuperclass($parentClass)) {
  313.                     continue;
  314.                 }
  315.                 // We have found a non-transient, non-mapped-superclass = an entity class (possibly abstract, but that does not matter)
  316.                 $rootEntityClass->addSubClass($parentClass);
  317.             }
  318.         }
  319.     }
  320.     /** @param class-string $className */
  321.     private function peekIfIsMappedSuperclass(string $className): bool
  322.     {
  323.         $reflService $this->getReflectionService();
  324.         $class       $this->newClassMetadataInstance($className);
  325.         $this->initializeReflection($class$reflService);
  326.         $this->getDriver()->loadMetadataForClass($className$class);
  327.         return $class->isMappedSuperclass;
  328.     }
  329.     /**
  330.      * Gets the lower-case short name of a class.
  331.      *
  332.      * @psalm-param class-string $className
  333.      */
  334.     private function getShortName(string $className): string
  335.     {
  336.         if (! str_contains($className'\\')) {
  337.             return strtolower($className);
  338.         }
  339.         $parts explode('\\'$className);
  340.         return strtolower(end($parts));
  341.     }
  342.     /**
  343.      * Puts the `inherited` and `declared` values into mapping information for fields, associations
  344.      * and embedded classes.
  345.      */
  346.     private function addMappingInheritanceInformation(
  347.         AssociationMapping|EmbeddedClassMapping|FieldMapping $mapping,
  348.         ClassMetadata $parentClass,
  349.     ): void {
  350.         if (! isset($mapping->inherited) && ! $parentClass->isMappedSuperclass) {
  351.             $mapping->inherited $parentClass->name;
  352.         }
  353.         if (! isset($mapping->declared)) {
  354.             $mapping->declared $parentClass->name;
  355.         }
  356.     }
  357.     /**
  358.      * Adds inherited fields to the subclass mapping.
  359.      */
  360.     private function addInheritedFields(ClassMetadata $subClassClassMetadata $parentClass): void
  361.     {
  362.         foreach ($parentClass->fieldMappings as $mapping) {
  363.             $subClassMapping = clone $mapping;
  364.             $this->addMappingInheritanceInformation($subClassMapping$parentClass);
  365.             $subClass->addInheritedFieldMapping($subClassMapping);
  366.         }
  367.         foreach ($parentClass->reflFields as $name => $field) {
  368.             $subClass->reflFields[$name] = $field;
  369.         }
  370.     }
  371.     /**
  372.      * Adds inherited association mappings to the subclass mapping.
  373.      *
  374.      * @throws MappingException
  375.      */
  376.     private function addInheritedRelations(ClassMetadata $subClassClassMetadata $parentClass): void
  377.     {
  378.         foreach ($parentClass->associationMappings as $field => $mapping) {
  379.             $subClassMapping = clone $mapping;
  380.             $this->addMappingInheritanceInformation($subClassMapping$parentClass);
  381.             // When the class inheriting the relation ($subClass) is the first entity class since the
  382.             // relation has been defined in a mapped superclass (or in a chain
  383.             // of mapped superclasses) above, then declare this current entity class as the source of
  384.             // the relationship.
  385.             // According to the definitions given in https://github.com/doctrine/orm/pull/10396/,
  386.             // this is the case <=> ! isset($mapping['inherited']).
  387.             if (! isset($subClassMapping->inherited)) {
  388.                 $subClassMapping->sourceEntity $subClass->name;
  389.             }
  390.             $subClass->addInheritedAssociationMapping($subClassMapping);
  391.         }
  392.     }
  393.     private function addInheritedEmbeddedClasses(ClassMetadata $subClassClassMetadata $parentClass): void
  394.     {
  395.         foreach ($parentClass->embeddedClasses as $field => $embeddedClass) {
  396.             $subClassMapping = clone $embeddedClass;
  397.             $this->addMappingInheritanceInformation($subClassMapping$parentClass);
  398.             $subClass->embeddedClasses[$field] = $subClassMapping;
  399.         }
  400.     }
  401.     /**
  402.      * Adds nested embedded classes metadata to a parent class.
  403.      *
  404.      * @param ClassMetadata $subClass    Sub embedded class metadata to add nested embedded classes metadata from.
  405.      * @param ClassMetadata $parentClass Parent class to add nested embedded classes metadata to.
  406.      * @param string        $prefix      Embedded classes' prefix to use for nested embedded classes field names.
  407.      */
  408.     private function addNestedEmbeddedClasses(
  409.         ClassMetadata $subClass,
  410.         ClassMetadata $parentClass,
  411.         string $prefix,
  412.     ): void {
  413.         foreach ($subClass->embeddedClasses as $property => $embeddableClass) {
  414.             if (isset($embeddableClass->inherited)) {
  415.                 continue;
  416.             }
  417.             $embeddableMetadata $this->getMetadataFor($embeddableClass->class);
  418.             $parentClass->mapEmbedded(
  419.                 [
  420.                     'fieldName' => $prefix '.' $property,
  421.                     'class' => $embeddableMetadata->name,
  422.                     'columnPrefix' => $embeddableClass->columnPrefix,
  423.                     'declaredField' => $embeddableClass->declaredField
  424.                             $prefix '.' $embeddableClass->declaredField
  425.                             $prefix,
  426.                     'originalField' => $embeddableClass->originalField ?: $property,
  427.                 ],
  428.             );
  429.         }
  430.     }
  431.     /**
  432.      * Copy the table indices from the parent class superclass to the child class
  433.      */
  434.     private function addInheritedIndexes(ClassMetadata $subClassClassMetadata $parentClass): void
  435.     {
  436.         if (! $parentClass->isMappedSuperclass) {
  437.             return;
  438.         }
  439.         foreach (['uniqueConstraints''indexes'] as $indexType) {
  440.             if (isset($parentClass->table[$indexType])) {
  441.                 foreach ($parentClass->table[$indexType] as $indexName => $index) {
  442.                     if (isset($subClass->table[$indexType][$indexName])) {
  443.                         continue; // Let the inheriting table override indices
  444.                     }
  445.                     $subClass->table[$indexType][$indexName] = $index;
  446.                 }
  447.             }
  448.         }
  449.     }
  450.     /**
  451.      * Completes the ID generator mapping. If "auto" is specified we choose the generator
  452.      * most appropriate for the targeted database platform.
  453.      *
  454.      * @throws ORMException
  455.      */
  456.     private function completeIdGeneratorMapping(ClassMetadata $class): void
  457.     {
  458.         $idGenType $class->generatorType;
  459.         if ($idGenType === ClassMetadata::GENERATOR_TYPE_AUTO) {
  460.             $class->setIdGeneratorType($this->determineIdGeneratorStrategy($this->getTargetPlatform()));
  461.         }
  462.         // Create & assign an appropriate ID generator instance
  463.         switch ($class->generatorType) {
  464.             case ClassMetadata::GENERATOR_TYPE_IDENTITY:
  465.                 $sequenceName null;
  466.                 $fieldName    $class->identifier $class->getSingleIdentifierFieldName() : null;
  467.                 $platform     $this->getTargetPlatform();
  468.                 $generator $fieldName && $class->fieldMappings[$fieldName]->type === 'bigint'
  469.                     ? new BigIntegerIdentityGenerator()
  470.                     : new IdentityGenerator();
  471.                 $class->setIdGenerator($generator);
  472.                 break;
  473.             case ClassMetadata::GENERATOR_TYPE_SEQUENCE:
  474.                 // If there is no sequence definition yet, create a default definition
  475.                 $definition $class->sequenceGeneratorDefinition;
  476.                 if (! $definition) {
  477.                     $fieldName    $class->getSingleIdentifierFieldName();
  478.                     $sequenceName $class->getSequenceName($this->getTargetPlatform());
  479.                     $quoted       = isset($class->fieldMappings[$fieldName]->quoted) || isset($class->table['quoted']);
  480.                     $definition = [
  481.                         'sequenceName'      => $this->truncateSequenceName($sequenceName),
  482.                         'allocationSize'    => 1,
  483.                         'initialValue'      => 1,
  484.                     ];
  485.                     if ($quoted) {
  486.                         $definition['quoted'] = true;
  487.                     }
  488.                     $class->setSequenceGeneratorDefinition($definition);
  489.                 }
  490.                 $sequenceGenerator = new SequenceGenerator(
  491.                     $this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition$class$this->getTargetPlatform()),
  492.                     (int) $definition['allocationSize'],
  493.                 );
  494.                 $class->setIdGenerator($sequenceGenerator);
  495.                 break;
  496.             case ClassMetadata::GENERATOR_TYPE_NONE:
  497.                 $class->setIdGenerator(new AssignedGenerator());
  498.                 break;
  499.             case ClassMetadata::GENERATOR_TYPE_CUSTOM:
  500.                 $definition $class->customGeneratorDefinition;
  501.                 if ($definition === null) {
  502.                     throw InvalidCustomGenerator::onClassNotConfigured();
  503.                 }
  504.                 if (! class_exists($definition['class'])) {
  505.                     throw InvalidCustomGenerator::onMissingClass($definition);
  506.                 }
  507.                 $class->setIdGenerator(new $definition['class']());
  508.                 break;
  509.             default:
  510.                 throw UnknownGeneratorType::create($class->generatorType);
  511.         }
  512.     }
  513.     /** @psalm-return ClassMetadata::GENERATOR_TYPE_* */
  514.     private function determineIdGeneratorStrategy(AbstractPlatform $platform): int
  515.     {
  516.         assert($this->em !== null);
  517.         foreach ($this->em->getConfiguration()->getIdentityGenerationPreferences() as $platformFamily => $strategy) {
  518.             if (is_a($platform$platformFamily)) {
  519.                 return $strategy;
  520.             }
  521.         }
  522.         $nonIdentityDefaultStrategy self::NON_IDENTITY_DEFAULT_STRATEGY;
  523.         // DBAL 3
  524.         if (method_exists($platform'getIdentitySequenceName')) {
  525.             $nonIdentityDefaultStrategy[Platforms\PostgreSQLPlatform::class] = ClassMetadata::GENERATOR_TYPE_SEQUENCE;
  526.         }
  527.         foreach ($nonIdentityDefaultStrategy as $platformFamily => $strategy) {
  528.             if (is_a($platform$platformFamily)) {
  529.                 if ($platform instanceof Platforms\PostgreSQLPlatform) {
  530.                     Deprecation::trigger(
  531.                         'doctrine/orm',
  532.                         'https://github.com/doctrine/orm/issues/8893',
  533.                         <<<'DEPRECATION'
  534.                         Relying on non-optimal defaults for ID generation is deprecated, and IDENTITY
  535.                         results in SERIAL, which is not recommended.
  536.                         Instead, configure identifier generation strategies explicitly through
  537.                         configuration.
  538.                         We currently recommend "SEQUENCE" for "%s", when using DBAL 3,
  539.                         and "IDENTITY" when using DBAL 4,
  540.                         so you should use probably use the following configuration before upgrading to DBAL 4,
  541.                         and remove it after deploying that upgrade:
  542.                         $configuration->setIdentityGenerationPreferences([
  543.                             "%s" => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
  544.                         ]);
  545.                         DEPRECATION,
  546.                         $platformFamily,
  547.                         $platformFamily,
  548.                     );
  549.                 }
  550.                 return $strategy;
  551.             }
  552.         }
  553.         return ClassMetadata::GENERATOR_TYPE_IDENTITY;
  554.     }
  555.     private function truncateSequenceName(string $schemaElementName): string
  556.     {
  557.         $platform $this->getTargetPlatform();
  558.         if (! $platform instanceof Platforms\OraclePlatform) {
  559.             return $schemaElementName;
  560.         }
  561.         $maxIdentifierLength $platform->getMaxIdentifierLength();
  562.         if (strlen($schemaElementName) > $maxIdentifierLength) {
  563.             return substr($schemaElementName0$maxIdentifierLength);
  564.         }
  565.         return $schemaElementName;
  566.     }
  567.     /**
  568.      * Inherits the ID generator mapping from a parent class.
  569.      */
  570.     private function inheritIdGeneratorMapping(ClassMetadata $classClassMetadata $parent): void
  571.     {
  572.         if ($parent->isIdGeneratorSequence()) {
  573.             $class->setSequenceGeneratorDefinition($parent->sequenceGeneratorDefinition);
  574.         }
  575.         if ($parent->generatorType) {
  576.             $class->setIdGeneratorType($parent->generatorType);
  577.         }
  578.         if ($parent->idGenerator ?? null) {
  579.             $class->setIdGenerator($parent->idGenerator);
  580.         }
  581.     }
  582.     protected function wakeupReflection(ClassMetadataInterface $classReflectionService $reflService): void
  583.     {
  584.         $class->wakeupReflection($reflService);
  585.     }
  586.     protected function initializeReflection(ClassMetadataInterface $classReflectionService $reflService): void
  587.     {
  588.         $class->initializeReflection($reflService);
  589.     }
  590.     protected function getDriver(): MappingDriver
  591.     {
  592.         assert($this->driver !== null);
  593.         return $this->driver;
  594.     }
  595.     protected function isEntity(ClassMetadataInterface $class): bool
  596.     {
  597.         return ! $class->isMappedSuperclass;
  598.     }
  599.     private function getTargetPlatform(): Platforms\AbstractPlatform
  600.     {
  601.         if (! $this->targetPlatform) {
  602.             $this->targetPlatform $this->em->getConnection()->getDatabasePlatform();
  603.         }
  604.         return $this->targetPlatform;
  605.     }
  606. }