setIdGeneratorType() публичный Метод

Sets the type of Id generator to use for the mapped class.
public setIdGeneratorType ( $generatorType )
Пример #1
0
 public function generateBookXmlEntityFixture()
 {
     $metadata = new ClassMetadataInfo($this->namespace . '\\XmlEntityGeneratorBook');
     $metadata->customRepositoryClassName = $this->namespace . '\\XmlEntityGeneratorBookRepository';
     $metadata->isRoot = true;
     $metadata->mapField(array('fieldName' => 'name', 'type' => 'string'));
     $metadata->mapField(array('fieldName' => 'status', 'type' => 'string'));
     $metadata->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
     $metadata->addLifecycleCallback('loading', 'postLoad');
     $metadata->addLifecycleCallback('willBeRemoved', 'preRemove');
     $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
     $this->generator->writeXmlEntityClass($metadata, $this->tmpDir);
     return $metadata;
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
 {
     $reflClass = $metadata->getReflectionClass();
     $classAnnotations = $this->reader->getClassAnnotations($reflClass);
     // Compatibility with Doctrine Common 3.x
     if ($classAnnotations && is_int(key($classAnnotations))) {
         foreach ($classAnnotations as $annot) {
             $classAnnotations[get_class($annot)] = $annot;
         }
     }
     // Evaluate XmlEntity Annotations
     if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlEntity'])) {
         $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlEntity'];
     } elseif (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlRootEntity'])) {
         $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlRootEntity'];
         $metadata->isRoot = true;
     } elseif (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlMappedSuperclass'])) {
         $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlMappedSuperclass'];
         $metadata->isMappedSuperclass = true;
     } else {
         throw MappingException::classIsNotAValidXmlEntity($className);
     }
     $metadata->setName($reflClass->getName());
     if (isset($entityAnnot->xml)) {
         $metadata->setXmlName($entityAnnot->xml);
     } else {
         $metadata->setXmlName(Inflector::xmlize($reflClass->getShortName()));
     }
     if (isset($entityAnnot->repositoryClass)) {
         $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
     }
     // Evaluate XmlChangeTrackingPolicy annotation
     if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlChangeTrackingPolicy'])) {
         $changeTrackingAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlChangeTrackingPolicy'];
         $metadata->setChangeTrackingPolicy(constant('Doctrine\\OXM\\Mapping\\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value));
     }
     // Check for XmlNamespace/XmlNamespaces annotations
     $xmlNamespaces = array();
     if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespace'])) {
         $xmlNamespaceAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespace'];
         $xmlNamespaces[] = array('url' => $xmlNamespaceAnnot->url, 'prefix' => $xmlNamespaceAnnot->prefix);
     } else {
         if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespaces'])) {
             $xmlNamespaceAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespaces'];
             foreach ($xmlNamespaceAnnot->value as $xmlNamespace) {
                 $xmlNamespaces[] = array('url' => $xmlNamespace->url, 'prefix' => $xmlNamespace->prefix);
             }
         }
     }
     $metadata->setXmlNamespaces($xmlNamespaces);
     foreach ($reflClass->getProperties() as $property) {
         if ($metadata->isMappedSuperclass && !$property->isPrivate() || $metadata->isInheritedField($property->name)) {
             continue;
         }
         $mapping = array();
         $mapping['fieldName'] = $property->getName();
         if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlId')) {
             $mapping['id'] = true;
         }
         if ($generatedValueAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlGeneratedValue')) {
             $metadata->setIdGeneratorType(constant('Doctrine\\OXM\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy));
         }
         $referenceAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlReferences');
         if (isset($referenceAnnot->entityName)) {
             $mapping['references'] = $referenceAnnot->entityName;
         }
         // todo add Id Generator strategy support
         foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) {
             if ($fieldAnnot instanceof \Doctrine\OXM\Mapping\XmlField) {
                 if ($fieldAnnot->type == null) {
                     throw MappingException::propertyTypeIsRequired($className, $property->getName());
                 }
                 $mapping = array_merge($mapping, (array) $fieldAnnot);
                 $metadata->mapField($mapping);
             }
         }
     }
     // Evaluate @HasLifecycleCallbacks annotations
     if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\HasLifecycleCallbacks'])) {
         foreach ($reflClass->getMethods() as $method) {
             // filter for the declaring class only, callbacks from parents will already be registered.
             if ($method->isPublic() && $method->getDeclaringClass()->getName() == $reflClass->name) {
                 $annotations = $this->reader->getMethodAnnotations($method);
                 // Compatibility with Doctrine Common 3.x
                 if ($annotations && is_int(key($annotations))) {
                     foreach ($annotations as $annot) {
                         $annotations[get_class($annot)] = $annot;
                     }
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PreMarshal'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preMarshal);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PostMarshal'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postMarshal);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PreUnmarshal'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preUnmarshal);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PostUnmarshal'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postUnmarshal);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PrePersist'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::prePersist);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PostPersist'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postPersist);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PreUpdate'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preUpdate);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PostUpdate'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postUpdate);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PreRemove'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preRemove);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PostRemove'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postRemove);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PreLoad'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preLoad);
                 }
                 if (isset($annotations['Doctrine\\OXM\\Mapping\\PostLoad'])) {
                     $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postLoad);
                 }
             }
         }
     }
 }
Пример #3
0
 /**
  * Completes the ID generator mapping. If "auto" is specified we choose the generator
  * most appropriate.
  *
  * @param Doctrine\OXM\Mapping\ClassMetadataInfo $class
  */
 private function completeIdGeneratorMapping(ClassMetadataInfo $class)
 {
     $idGenType = $class->generatorType;
     if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
         $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
     }
     // Create & assign an appropriate ID generator instance
     switch ($class->generatorType) {
         case ClassMetadataInfo::GENERATOR_TYPE_INCREMENT:
             throw new OXMException("Increment generator type not implemented yet");
             break;
         case ClassMetadataInfo::GENERATOR_TYPE_NONE:
             $class->setIdGenerator(new \Doctrine\OXM\Id\AssignedGenerator());
             break;
         case ClassMetadataInfo::GENERATOR_TYPE_UUID:
             $class->setIdGenerator(new \Doctrine\OXM\Id\UuidGenerator());
             break;
         default:
             throw new OXMException("Unknown generator type: " . $class->generatorType);
     }
 }