vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php line 248

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\ORM\Mapping;
  20. use Doctrine\DBAL\Platforms;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
  23. use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs;
  24. use Doctrine\ORM\Events;
  25. use Doctrine\ORM\Id\BigIntegerIdentityGenerator;
  26. use Doctrine\ORM\Id\IdentityGenerator;
  27. use Doctrine\ORM\ORMException;
  28. use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
  29. use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
  30. use Doctrine\Persistence\Mapping\Driver\MappingDriver;
  31. use Doctrine\Persistence\Mapping\ReflectionService;
  32. use ReflectionClass;
  33. use ReflectionException;
  34. use function interface_exists;
  35. /**
  36.  * The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
  37.  * metadata mapping information of a class which describes how a class should be mapped
  38.  * to a relational database.
  39.  *
  40.  * @since   2.0
  41.  * @author  Benjamin Eberlei <kontakt@beberlei.de>
  42.  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
  43.  * @author  Jonathan Wage <jonwage@gmail.com>
  44.  * @author  Roman Borschel <roman@code-factory.org>
  45.  */
  46. class ClassMetadataFactory extends AbstractClassMetadataFactory
  47. {
  48.     /**
  49.      * @var EntityManagerInterface|null
  50.      */
  51.     private $em;
  52.     /**
  53.      * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  54.      */
  55.     private $targetPlatform;
  56.     /**
  57.      * @var MappingDriver
  58.      */
  59.     private $driver;
  60.     /**
  61.      * @var \Doctrine\Common\EventManager
  62.      */
  63.     private $evm;
  64.     /**
  65.      * @var array
  66.      */
  67.     private $embeddablesActiveNesting = [];
  68.     /**
  69.      * {@inheritDoc}
  70.      */
  71.     protected function loadMetadata($name)
  72.     {
  73.         $loaded parent::loadMetadata($name);
  74.         array_map([$this'resolveDiscriminatorValue'], array_map([$this'getMetadataFor'], $loaded));
  75.         return $loaded;
  76.     }
  77.     /**
  78.      * @param EntityManagerInterface $em
  79.      */
  80.     public function setEntityManager(EntityManagerInterface $em)
  81.     {
  82.         $this->em $em;
  83.     }
  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     protected function initialize()
  88.     {
  89.         $this->driver $this->em->getConfiguration()->getMetadataDriverImpl();
  90.         $this->evm $this->em->getEventManager();
  91.         $this->initialized true;
  92.     }
  93.     /**
  94.      * {@inheritDoc}
  95.      */
  96.     protected function onNotFoundMetadata($className)
  97.     {
  98.         if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
  99.             return;
  100.         }
  101.         $eventArgs = new OnClassMetadataNotFoundEventArgs($className$this->em);
  102.         $this->evm->dispatchEvent(Events::onClassMetadataNotFound$eventArgs);
  103.         return $eventArgs->getFoundMetadata();
  104.     }
  105.     /**
  106.      * {@inheritDoc}
  107.      */
  108.     protected function doLoadMetadata($class$parent$rootEntityFound, array $nonSuperclassParents)
  109.     {
  110.         /* @var $class ClassMetadata */
  111.         /* @var $parent ClassMetadata */
  112.         if ($parent) {
  113.             $class->setInheritanceType($parent->inheritanceType);
  114.             $class->setDiscriminatorColumn($parent->discriminatorColumn);
  115.             $class->setIdGeneratorType($parent->generatorType);
  116.             $this->addInheritedFields($class$parent);
  117.             $this->addInheritedRelations($class$parent);
  118.             $this->addInheritedEmbeddedClasses($class$parent);
  119.             $class->setIdentifier($parent->identifier);
  120.             $class->setVersioned($parent->isVersioned);
  121.             $class->setVersionField($parent->versionField);
  122.             $class->setDiscriminatorMap($parent->discriminatorMap);
  123.             $class->setLifecycleCallbacks($parent->lifecycleCallbacks);
  124.             $class->setChangeTrackingPolicy($parent->changeTrackingPolicy);
  125.             if ( ! empty($parent->customGeneratorDefinition)) {
  126.                 $class->setCustomGeneratorDefinition($parent->customGeneratorDefinition);
  127.             }
  128.             if ($parent->isMappedSuperclass) {
  129.                 $class->setCustomRepositoryClass($parent->customRepositoryClassName);
  130.             }
  131.         }
  132.         // Invoke driver
  133.         try {
  134.             $this->driver->loadMetadataForClass($class->getName(), $class);
  135.         } catch (ReflectionException $e) {
  136.             throw MappingException::reflectionFailure($class->getName(), $e);
  137.         }
  138.         // If this class has a parent the id generator strategy is inherited.
  139.         // However this is only true if the hierarchy of parents contains the root entity,
  140.         // if it consists of mapped superclasses these don't necessarily include the id field.
  141.         if ($parent && $rootEntityFound) {
  142.             $this->inheritIdGeneratorMapping($class$parent);
  143.         } else {
  144.             $this->completeIdGeneratorMapping($class);
  145.         }
  146.         if (!$class->isMappedSuperclass) {
  147.             foreach ($class->embeddedClasses as $property => $embeddableClass) {
  148.                 if (isset($embeddableClass['inherited'])) {
  149.                     continue;
  150.                 }
  151.                 if ( ! (isset($embeddableClass['class']) && $embeddableClass['class'])) {
  152.                     throw MappingException::missingEmbeddedClass($property);
  153.                 }
  154.                 if (isset($this->embeddablesActiveNesting[$embeddableClass['class']])) {
  155.                     throw MappingException::infiniteEmbeddableNesting($class->name$property);
  156.                 }
  157.                 $this->embeddablesActiveNesting[$class->name] = true;
  158.                 $embeddableMetadata $this->getMetadataFor($embeddableClass['class']);
  159.                 if ($embeddableMetadata->isEmbeddedClass) {
  160.                     $this->addNestedEmbeddedClasses($embeddableMetadata$class$property);
  161.                 }
  162.                 $identifier $embeddableMetadata->getIdentifier();
  163.                 if (! empty($identifier)) {
  164.                     $this->inheritIdGeneratorMapping($class$embeddableMetadata);
  165.                 }
  166.                 $class->inlineEmbeddable($property$embeddableMetadata);
  167.                 unset($this->embeddablesActiveNesting[$class->name]);
  168.             }
  169.         }
  170.         if ($parent) {
  171.             if ($parent->isInheritanceTypeSingleTable()) {
  172.                 $class->setPrimaryTable($parent->table);
  173.             }
  174.             if ($parent) {
  175.                 $this->addInheritedIndexes($class$parent);
  176.             }
  177.             if ($parent->cache) {
  178.                 $class->cache $parent->cache;
  179.             }
  180.             if ($parent->containsForeignIdentifier) {
  181.                 $class->containsForeignIdentifier true;
  182.             }
  183.             if ( ! empty($parent->namedQueries)) {
  184.                 $this->addInheritedNamedQueries($class$parent);
  185.             }
  186.             if ( ! empty($parent->namedNativeQueries)) {
  187.                 $this->addInheritedNamedNativeQueries($class$parent);
  188.             }
  189.             if ( ! empty($parent->sqlResultSetMappings)) {
  190.                 $this->addInheritedSqlResultSetMappings($class$parent);
  191.             }
  192.             if ( ! empty($parent->entityListeners) && empty($class->entityListeners)) {
  193.                 $class->entityListeners $parent->entityListeners;
  194.             }
  195.         }
  196.         $class->setParentClasses($nonSuperclassParents);
  197.         if ($class->isRootEntity() && ! $class->isInheritanceTypeNone() && ! $class->discriminatorMap) {
  198.             $this->addDefaultDiscriminatorMap($class);
  199.         }
  200.         if ($this->evm->hasListeners(Events::loadClassMetadata)) {
  201.             $eventArgs = new LoadClassMetadataEventArgs($class$this->em);
  202.             $this->evm->dispatchEvent(Events::loadClassMetadata$eventArgs);
  203.         }
  204.         $this->validateRuntimeMetadata($class$parent);
  205.     }
  206.     /**
  207.      * Validate runtime metadata is correctly defined.
  208.      *
  209.      * @param ClassMetadata               $class
  210.      * @param ClassMetadataInterface|null $parent
  211.      *
  212.      * @return void
  213.      *
  214.      * @throws MappingException
  215.      */
  216.     protected function validateRuntimeMetadata($class$parent)
  217.     {
  218.         if ( ! $class->reflClass ) {
  219.             // only validate if there is a reflection class instance
  220.             return;
  221.         }
  222.         $class->validateIdentifier();
  223.         $class->validateAssociations();
  224.         $class->validateLifecycleCallbacks($this->getReflectionService());
  225.         // verify inheritance
  226.         if ( ! $class->isMappedSuperclass && !$class->isInheritanceTypeNone()) {
  227.             if ( ! $parent) {
  228.                 if (count($class->discriminatorMap) == 0) {
  229.                     throw MappingException::missingDiscriminatorMap($class->name);
  230.                 }
  231.                 if ( ! $class->discriminatorColumn) {
  232.                     throw MappingException::missingDiscriminatorColumn($class->name);
  233.                 }
  234.                 foreach ($class->subClasses as $subClass) {
  235.                     if ((new ReflectionClass($subClass))->name !== $subClass) {
  236.                         throw MappingException::invalidClassInDiscriminatorMap($subClass$class->name);
  237.                     }
  238.                 }
  239.             }
  240.         } else if ($class->isMappedSuperclass && $class->name == $class->rootEntityName && (count($class->discriminatorMap) || $class->discriminatorColumn)) {
  241.             // second condition is necessary for mapped superclasses in the middle of an inheritance hierarchy
  242.             throw MappingException::noInheritanceOnMappedSuperClass($class->name);
  243.         }
  244.     }
  245.     /**
  246.      * {@inheritDoc}
  247.      */
  248.     protected function newClassMetadataInstance($className)
  249.     {
  250.         return new ClassMetadata($className$this->em->getConfiguration()->getNamingStrategy());
  251.     }
  252.     /**
  253.      * Populates the discriminator value of the given metadata (if not set) by iterating over discriminator
  254.      * map classes and looking for a fitting one.
  255.      *
  256.      * @param ClassMetadata $metadata
  257.      *
  258.      * @return void
  259.      *
  260.      * @throws MappingException
  261.      */
  262.     private function resolveDiscriminatorValue(ClassMetadata $metadata)
  263.     {
  264.         if ($metadata->discriminatorValue
  265.             || ! $metadata->discriminatorMap
  266.             || $metadata->isMappedSuperclass
  267.             || ! $metadata->reflClass
  268.             || $metadata->reflClass->isAbstract()
  269.         ) {
  270.             return;
  271.         }
  272.         // minor optimization: avoid loading related metadata when not needed
  273.         foreach ($metadata->discriminatorMap as $discriminatorValue => $discriminatorClass) {
  274.             if ($discriminatorClass === $metadata->name) {
  275.                 $metadata->discriminatorValue $discriminatorValue;
  276.                 return;
  277.             }
  278.         }
  279.         // iterate over discriminator mappings and resolve actual referenced classes according to existing metadata
  280.         foreach ($metadata->discriminatorMap as $discriminatorValue => $discriminatorClass) {
  281.             if ($metadata->name === $this->getMetadataFor($discriminatorClass)->getName()) {
  282.                 $metadata->discriminatorValue $discriminatorValue;
  283.                 return;
  284.             }
  285.         }
  286.         throw MappingException::mappedClassNotPartOfDiscriminatorMap($metadata->name$metadata->rootEntityName);
  287.     }
  288.     /**
  289.      * Adds a default discriminator map if no one is given
  290.      *
  291.      * If an entity is of any inheritance type and does not contain a
  292.      * discriminator map, then the map is generated automatically. This process
  293.      * is expensive computation wise.
  294.      *
  295.      * The automatically generated discriminator map contains the lowercase short name of
  296.      * each class as key.
  297.      *
  298.      * @param \Doctrine\ORM\Mapping\ClassMetadata $class
  299.      *
  300.      * @throws MappingException
  301.      */
  302.     private function addDefaultDiscriminatorMap(ClassMetadata $class)
  303.     {
  304.         $allClasses $this->driver->getAllClassNames();
  305.         $fqcn $class->getName();
  306.         $map = [$this->getShortName($class->name) => $fqcn];
  307.         $duplicates = [];
  308.         foreach ($allClasses as $subClassCandidate) {
  309.             if (is_subclass_of($subClassCandidate$fqcn)) {
  310.                 $shortName $this->getShortName($subClassCandidate);
  311.                 if (isset($map[$shortName])) {
  312.                     $duplicates[] = $shortName;
  313.                 }
  314.                 $map[$shortName] = $subClassCandidate;
  315.             }
  316.         }
  317.         if ($duplicates) {
  318.             throw MappingException::duplicateDiscriminatorEntry($class->name$duplicates$map);
  319.         }
  320.         $class->setDiscriminatorMap($map);
  321.     }
  322.     /**
  323.      * Gets the lower-case short name of a class.
  324.      *
  325.      * @param string $className
  326.      *
  327.      * @return string
  328.      */
  329.     private function getShortName($className)
  330.     {
  331.         if (strpos($className"\\") === false) {
  332.             return strtolower($className);
  333.         }
  334.         $parts explode("\\"$className);
  335.         return strtolower(end($parts));
  336.     }
  337.     /**
  338.      * Adds inherited fields to the subclass mapping.
  339.      *
  340.      * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
  341.      * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
  342.      *
  343.      * @return void
  344.      */
  345.     private function addInheritedFields(ClassMetadata $subClassClassMetadata $parentClass)
  346.     {
  347.         foreach ($parentClass->fieldMappings as $mapping) {
  348.             if (! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
  349.                 $mapping['inherited'] = $parentClass->name;
  350.             }
  351.             if (! isset($mapping['declared'])) {
  352.                 $mapping['declared'] = $parentClass->name;
  353.             }
  354.             $subClass->addInheritedFieldMapping($mapping);
  355.         }
  356.         foreach ($parentClass->reflFields as $name => $field) {
  357.             $subClass->reflFields[$name] = $field;
  358.         }
  359.     }
  360.     /**
  361.      * Adds inherited association mappings to the subclass mapping.
  362.      *
  363.      * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
  364.      * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
  365.      *
  366.      * @return void
  367.      *
  368.      * @throws MappingException
  369.      */
  370.     private function addInheritedRelations(ClassMetadata $subClassClassMetadata $parentClass)
  371.     {
  372.         foreach ($parentClass->associationMappings as $field => $mapping) {
  373.             if ($parentClass->isMappedSuperclass) {
  374.                 if ($mapping['type'] & ClassMetadata::TO_MANY && !$mapping['isOwningSide']) {
  375.                     throw MappingException::illegalToManyAssociationOnMappedSuperclass($parentClass->name$field);
  376.                 }
  377.                 $mapping['sourceEntity'] = $subClass->name;
  378.             }
  379.             //$subclassMapping = $mapping;
  380.             if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
  381.                 $mapping['inherited'] = $parentClass->name;
  382.             }
  383.             if ( ! isset($mapping['declared'])) {
  384.                 $mapping['declared'] = $parentClass->name;
  385.             }
  386.             $subClass->addInheritedAssociationMapping($mapping);
  387.         }
  388.     }
  389.     private function addInheritedEmbeddedClasses(ClassMetadata $subClassClassMetadata $parentClass)
  390.     {
  391.         foreach ($parentClass->embeddedClasses as $field => $embeddedClass) {
  392.             if ( ! isset($embeddedClass['inherited']) && ! $parentClass->isMappedSuperclass) {
  393.                 $embeddedClass['inherited'] = $parentClass->name;
  394.             }
  395.             if ( ! isset($embeddedClass['declared'])) {
  396.                 $embeddedClass['declared'] = $parentClass->name;
  397.             }
  398.             $subClass->embeddedClasses[$field] = $embeddedClass;
  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(ClassMetadata $subClassClassMetadata $parentClass$prefix)
  409.     {
  410.         foreach ($subClass->embeddedClasses as $property => $embeddableClass) {
  411.             if (isset($embeddableClass['inherited'])) {
  412.                 continue;
  413.             }
  414.             $embeddableMetadata $this->getMetadataFor($embeddableClass['class']);
  415.             $parentClass->mapEmbedded(
  416.                 [
  417.                     'fieldName' => $prefix '.' $property,
  418.                     'class' => $embeddableMetadata->name,
  419.                     'columnPrefix' => $embeddableClass['columnPrefix'],
  420.                     'declaredField' => $embeddableClass['declaredField']
  421.                             ? $prefix '.' $embeddableClass['declaredField']
  422.                             : $prefix,
  423.                     'originalField' => $embeddableClass['originalField'] ?: $property,
  424.                 ]
  425.             );
  426.         }
  427.     }
  428.     /**
  429.      * Copy the table indices from the parent class superclass to the child class
  430.      *
  431.      * @param ClassMetadata $subClass
  432.      * @param ClassMetadata $parentClass
  433.      *
  434.      * @return void
  435.      */
  436.     private function addInheritedIndexes(ClassMetadata $subClassClassMetadata $parentClass)
  437.     {
  438.         if (! $parentClass->isMappedSuperclass) {
  439.             return;
  440.         }
  441.         foreach (['uniqueConstraints''indexes'] as $indexType) {
  442.             if (isset($parentClass->table[$indexType])) {
  443.                 foreach ($parentClass->table[$indexType] as $indexName => $index) {
  444.                     if (isset($subClass->table[$indexType][$indexName])) {
  445.                         continue; // Let the inheriting table override indices
  446.                     }
  447.                     $subClass->table[$indexType][$indexName] = $index;
  448.                 }
  449.             }
  450.         }
  451.     }
  452.     /**
  453.      * Adds inherited named queries to the subclass mapping.
  454.      *
  455.      * @since 2.2
  456.      *
  457.      * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
  458.      * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
  459.      *
  460.      * @return void
  461.      */
  462.     private function addInheritedNamedQueries(ClassMetadata $subClassClassMetadata $parentClass)
  463.     {
  464.         foreach ($parentClass->namedQueries as $name => $query) {
  465.             if ( ! isset ($subClass->namedQueries[$name])) {
  466.                 $subClass->addNamedQuery(
  467.                     [
  468.                         'name'  => $query['name'],
  469.                         'query' => $query['query']
  470.                     ]
  471.                 );
  472.             }
  473.         }
  474.     }
  475.     /**
  476.      * Adds inherited named native queries to the subclass mapping.
  477.      *
  478.      * @since 2.3
  479.      *
  480.      * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
  481.      * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
  482.      *
  483.      * @return void
  484.      */
  485.     private function addInheritedNamedNativeQueries(ClassMetadata $subClassClassMetadata $parentClass)
  486.     {
  487.         foreach ($parentClass->namedNativeQueries as $name => $query) {
  488.             if ( ! isset ($subClass->namedNativeQueries[$name])) {
  489.                 $subClass->addNamedNativeQuery(
  490.                     [
  491.                         'name'              => $query['name'],
  492.                         'query'             => $query['query'],
  493.                         'isSelfClass'       => $query['isSelfClass'],
  494.                         'resultSetMapping'  => $query['resultSetMapping'],
  495.                         'resultClass'       => $query['isSelfClass'] ? $subClass->name $query['resultClass'],
  496.                     ]
  497.                 );
  498.             }
  499.         }
  500.     }
  501.     /**
  502.      * Adds inherited sql result set mappings to the subclass mapping.
  503.      *
  504.      * @since 2.3
  505.      *
  506.      * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
  507.      * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
  508.      *
  509.      * @return void
  510.      */
  511.     private function addInheritedSqlResultSetMappings(ClassMetadata $subClassClassMetadata $parentClass)
  512.     {
  513.         foreach ($parentClass->sqlResultSetMappings as $name => $mapping) {
  514.             if ( ! isset ($subClass->sqlResultSetMappings[$name])) {
  515.                 $entities = [];
  516.                 foreach ($mapping['entities'] as $entity) {
  517.                     $entities[] = [
  518.                         'fields'                => $entity['fields'],
  519.                         'isSelfClass'           => $entity['isSelfClass'],
  520.                         'discriminatorColumn'   => $entity['discriminatorColumn'],
  521.                         'entityClass'           => $entity['isSelfClass'] ? $subClass->name $entity['entityClass'],
  522.                     ];
  523.                 }
  524.                 $subClass->addSqlResultSetMapping(
  525.                     [
  526.                         'name'          => $mapping['name'],
  527.                         'columns'       => $mapping['columns'],
  528.                         'entities'      => $entities,
  529.                     ]
  530.                 );
  531.             }
  532.         }
  533.     }
  534.     /**
  535.      * Completes the ID generator mapping. If "auto" is specified we choose the generator
  536.      * most appropriate for the targeted database platform.
  537.      *
  538.      * @param ClassMetadataInfo $class
  539.      *
  540.      * @return void
  541.      *
  542.      * @throws ORMException
  543.      */
  544.     private function completeIdGeneratorMapping(ClassMetadataInfo $class)
  545.     {
  546.         $idGenType $class->generatorType;
  547.         if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
  548.             if ($this->getTargetPlatform()->prefersSequences()) {
  549.                 $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE);
  550.             } else if ($this->getTargetPlatform()->prefersIdentityColumns()) {
  551.                 $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
  552.             } else {
  553.                 $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE);
  554.             }
  555.         }
  556.         // Create & assign an appropriate ID generator instance
  557.         switch ($class->generatorType) {
  558.             case ClassMetadata::GENERATOR_TYPE_IDENTITY:
  559.                 $sequenceName null;
  560.                 $fieldName    $class->identifier $class->getSingleIdentifierFieldName() : null;
  561.                 // Platforms that do not have native IDENTITY support need a sequence to emulate this behaviour.
  562.                 if ($this->getTargetPlatform()->usesSequenceEmulatedIdentityColumns()) {
  563.                     $columnName     $class->getSingleIdentifierColumnName();
  564.                     $quoted         = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
  565.                     $sequencePrefix $class->getSequencePrefix($this->getTargetPlatform());
  566.                     $sequenceName   $this->getTargetPlatform()->getIdentitySequenceName($sequencePrefix$columnName);
  567.                     $definition     = [
  568.                         'sequenceName' => $this->getTargetPlatform()->fixSchemaElementName($sequenceName)
  569.                     ];
  570.                     if ($quoted) {
  571.                         $definition['quoted'] = true;
  572.                     }
  573.                     $sequenceName $this
  574.                         ->em
  575.                         ->getConfiguration()
  576.                         ->getQuoteStrategy()
  577.                         ->getSequenceName($definition$class$this->getTargetPlatform());
  578.                 }
  579.                 $generator = ($fieldName && $class->fieldMappings[$fieldName]['type'] === 'bigint')
  580.                     ? new BigIntegerIdentityGenerator($sequenceName)
  581.                     : new IdentityGenerator($sequenceName);
  582.                 $class->setIdGenerator($generator);
  583.                 break;
  584.             case ClassMetadata::GENERATOR_TYPE_SEQUENCE:
  585.                 // If there is no sequence definition yet, create a default definition
  586.                 $definition $class->sequenceGeneratorDefinition;
  587.                 if ( ! $definition) {
  588.                     $fieldName      $class->getSingleIdentifierFieldName();
  589.                     $sequenceName   $class->getSequenceName($this->getTargetPlatform());
  590.                     $quoted         = isset($class->fieldMappings[$fieldName]['quoted']) || isset($class->table['quoted']);
  591.                     $definition = [
  592.                         'sequenceName'      => $this->getTargetPlatform()->fixSchemaElementName($sequenceName),
  593.                         'allocationSize'    => 1,
  594.                         'initialValue'      => 1,
  595.                     ];
  596.                     if ($quoted) {
  597.                         $definition['quoted'] = true;
  598.                     }
  599.                     $class->setSequenceGeneratorDefinition($definition);
  600.                 }
  601.                 $sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator(
  602.                     $this->em->getConfiguration()->getQuoteStrategy()->getSequenceName($definition$class$this->getTargetPlatform()),
  603.                     $definition['allocationSize']
  604.                 );
  605.                 $class->setIdGenerator($sequenceGenerator);
  606.                 break;
  607.             case ClassMetadata::GENERATOR_TYPE_NONE:
  608.                 $class->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
  609.                 break;
  610.             case ClassMetadata::GENERATOR_TYPE_UUID:
  611.                 $class->setIdGenerator(new \Doctrine\ORM\Id\UuidGenerator());
  612.                 break;
  613.             case ClassMetadata::GENERATOR_TYPE_TABLE:
  614.                 throw new ORMException("TableGenerator not yet implemented.");
  615.                 break;
  616.             case ClassMetadata::GENERATOR_TYPE_CUSTOM:
  617.                 $definition $class->customGeneratorDefinition;
  618.                 if ($definition === null) {
  619.                     throw new ORMException("Can't instantiate custom generator : no custom generator definition");
  620.                 }
  621.                 if ( ! class_exists($definition['class'])) {
  622.                     throw new ORMException("Can't instantiate custom generator : " .
  623.                         $definition['class']);
  624.                 }
  625.                 $class->setIdGenerator(new $definition['class']);
  626.                 break;
  627.             default:
  628.                 throw new ORMException("Unknown generator type: " $class->generatorType);
  629.         }
  630.     }
  631.     /**
  632.      * Inherits the ID generator mapping from a parent class.
  633.      *
  634.      * @param ClassMetadataInfo $class
  635.      * @param ClassMetadataInfo $parent
  636.      */
  637.     private function inheritIdGeneratorMapping(ClassMetadataInfo $classClassMetadataInfo $parent)
  638.     {
  639.         if ($parent->isIdGeneratorSequence()) {
  640.             $class->setSequenceGeneratorDefinition($parent->sequenceGeneratorDefinition);
  641.         } elseif ($parent->isIdGeneratorTable()) {
  642.             $class->tableGeneratorDefinition $parent->tableGeneratorDefinition;
  643.         }
  644.         if ($parent->generatorType) {
  645.             $class->setIdGeneratorType($parent->generatorType);
  646.         }
  647.         if ($parent->idGenerator) {
  648.             $class->setIdGenerator($parent->idGenerator);
  649.         }
  650.     }
  651.     /**
  652.      * {@inheritDoc}
  653.      */
  654.     protected function wakeupReflection(ClassMetadataInterface $classReflectionService $reflService)
  655.     {
  656.         /* @var $class ClassMetadata */
  657.         $class->wakeupReflection($reflService);
  658.     }
  659.     /**
  660.      * {@inheritDoc}
  661.      */
  662.     protected function initializeReflection(ClassMetadataInterface $classReflectionService $reflService)
  663.     {
  664.         /* @var $class ClassMetadata */
  665.         $class->initializeReflection($reflService);
  666.     }
  667.     /**
  668.      * {@inheritDoc}
  669.      */
  670.     protected function getFqcnFromAlias($namespaceAlias$simpleClassName)
  671.     {
  672.         return $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' $simpleClassName;
  673.     }
  674.     /**
  675.      * {@inheritDoc}
  676.      */
  677.     protected function getDriver()
  678.     {
  679.         return $this->driver;
  680.     }
  681.     /**
  682.      * {@inheritDoc}
  683.      */
  684.     protected function isEntity(ClassMetadataInterface $class)
  685.     {
  686.         return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false;
  687.     }
  688.     /**
  689.      * @return Platforms\AbstractPlatform
  690.      */
  691.     private function getTargetPlatform()
  692.     {
  693.         if (!$this->targetPlatform) {
  694.             $this->targetPlatform $this->em->getConnection()->getDatabasePlatform();
  695.         }
  696.         return $this->targetPlatform;
  697.     }
  698. }