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

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