/** * @group DDC-3428 */ public function testSequenceGenerator() { $exporter = new XmlExporter(); $metadata = new ClassMetadata('entityTest'); $metadata->mapField(array("fieldName" => 'id', "type" => 'integer', "columnName" => 'id', "id" => true)); $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE); $metadata->setSequenceGeneratorDefinition(array('sequenceName' => 'seq_entity_test_id', 'allocationSize' => 5, 'initialValue' => 1)); $expectedFileContent = <<<'XML' <?xml version="1.0" encoding="utf-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd" > <entity name="entityTest"> <id name="id" type="integer" column="id"> <generator strategy="SEQUENCE"/> <sequence-generator sequence-name="seq_entity_test_id" allocation-size="5" initial-value="1"/> </id> </entity> </doctrine-mapping> XML; $this->assertXmlStringEqualsXmlString($expectedFileContent, $exporter->exportClassMetadata($metadata)); }
/** * @group DDC-2662 */ public function testQuotedSequenceName() { $cm = new ClassMetadata('Doctrine\\Tests\\Models\\CMS\\CmsUser'); $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService()); $cm->setSequenceGeneratorDefinition(array('sequenceName' => '`foo`')); $this->assertEquals(array('sequenceName' => 'foo', 'quoted' => true), $cm->sequenceGeneratorDefinition); }
/** * Completes the ID generator mapping. If "auto" is specified we choose the generator * most appropriate for the targeted database platform. * * @param Doctrine\ORM\Mapping\ClassMetadata $class */ private function _completeIdGeneratorMapping(ClassMetadata $class) { $idGenType = $class->generatorType; if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) { if ($this->_targetPlatform->prefersSequences()) { $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE); } else { if ($this->_targetPlatform->prefersIdentityColumns()) { $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); } else { $class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE); } } } // Create & assign an appropriate ID generator instance switch ($class->generatorType) { case ClassMetadata::GENERATOR_TYPE_IDENTITY: $class->setIdGenerator(new \Doctrine\ORM\Id\IdentityGenerator()); break; case ClassMetadata::GENERATOR_TYPE_SEQUENCE: // If there is no sequence definition yet, create a default definition $definition = $class->sequenceGeneratorDefinition; if (!$definition) { $sequenceName = $class->getTableName() . '_' . $class->getSingleIdentifierColumnName() . '_seq'; $definition['sequenceName'] = $this->_targetPlatform->fixSchemaElementName($sequenceName); $definition['allocationSize'] = 10; $definition['initialValue'] = 1; $class->setSequenceGeneratorDefinition($definition); } $sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator($definition['sequenceName'], $definition['allocationSize']); $class->setIdGenerator($sequenceGenerator); break; case ClassMetadata::GENERATOR_TYPE_NONE: $class->setIdGenerator(new \Doctrine\ORM\Id\Assigned()); break; case ClassMetadata::GENERATOR_TYPE_TABLE: throw new ORMException("TableGenerator not yet implemented."); break; default: throw new ORMException("Unknown generator type: " . $class->generatorType); } }
/** * Loads the metadata for the specified class into the provided container. */ public function loadMetadataForClass($className, ClassMetadata $metadata) { $annotClass = new \Addendum\ReflectionAnnotatedClass($className); // Evaluate DoctrineEntity annotation if (($entityAnnot = $annotClass->getAnnotation('DoctrineEntity')) === false) { throw DoctrineException::updateMe("{$className} is no entity."); } $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass); // Evaluate DoctrineTable annotation if ($tableAnnot = $annotClass->getAnnotation('DoctrineTable')) { $metadata->setPrimaryTable(array('name' => $tableAnnot->name, 'schema' => $tableAnnot->schema, 'catalog' => $tableAnnot->catalog)); } // Evaluate DoctrineInheritanceType annotation if ($inheritanceTypeAnnot = $annotClass->getAnnotation('DoctrineInheritanceType')) { $metadata->setInheritanceType($inheritanceTypeAnnot->value); } // Evaluate DoctrineDiscriminatorColumn annotation if ($discrColumnAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorColumn')) { $metadata->setDiscriminatorColumn(array('name' => $discrColumnAnnot->name, 'type' => $discrColumnAnnot->type, 'length' => $discrColumnAnnot->length)); } // Evaluate DoctrineDiscriminatorMap annotation if ($discrValueAnnot = $annotClass->getAnnotation('DoctrineDiscriminatorValue')) { $metadata->setDiscriminatorValue($discrValueAnnot->value); } // Evaluate DoctrineSubClasses annotation if ($subClassesAnnot = $annotClass->getAnnotation('DoctrineSubClasses')) { $metadata->setSubclasses($subClassesAnnot->value); } // Evaluate DoctrineChangeTrackingPolicy annotation if ($changeTrackingAnnot = $annotClass->getAnnotation('DoctrineChangeTrackingPolicy')) { $metadata->setChangeTrackingPolicy($changeTrackingAnnot->value); } // Evaluate annotations on properties/fields foreach ($annotClass->getProperties() as $property) { if ($metadata->hasField($property->getName())) { continue; } $mapping = array(); $mapping['fieldName'] = $property->getName(); // Check for DoctrineJoinColummn/DoctrineJoinColumns annotations $joinColumns = array(); if ($joinColumnAnnot = $property->getAnnotation('DoctrineJoinColumn')) { $joinColumns[] = array('name' => $joinColumnAnnot->name, 'referencedColumnName' => $joinColumnAnnot->referencedColumnName, 'unique' => $joinColumnAnnot->unique, 'nullable' => $joinColumnAnnot->nullable, 'onDelete' => $joinColumnAnnot->onDelete, 'onUpdate' => $joinColumnAnnot->onUpdate); } else { if ($joinColumnsAnnot = $property->getAnnotation('DoctrineJoinColumns')) { $joinColumns = $joinColumnsAnnot->value; } } // Field can only be annotated with one of: DoctrineColumn, // DoctrineOneToOne, DoctrineOneToMany, DoctrineManyToOne, DoctrineManyToMany if ($columnAnnot = $property->getAnnotation('DoctrineColumn')) { if ($columnAnnot->type == null) { throw DoctrineException::updateMe("Missing type on property " . $property->getName()); } $mapping['type'] = $columnAnnot->type; $mapping['length'] = $columnAnnot->length; $mapping['nullable'] = $columnAnnot->nullable; if ($idAnnot = $property->getAnnotation('DoctrineId')) { $mapping['id'] = true; } if ($generatedValueAnnot = $property->getAnnotation('DoctrineGeneratedValue')) { $metadata->setIdGeneratorType($generatedValueAnnot->strategy); } $metadata->mapField($mapping); // Check for SequenceGenerator/TableGenerator definition if ($seqGeneratorAnnot = $property->getAnnotation('DoctrineSequenceGenerator')) { $metadata->setSequenceGeneratorDefinition(array('sequenceName' => $seqGeneratorAnnot->sequenceName, 'allocationSize' => $seqGeneratorAnnot->allocationSize, 'initialValue' => $seqGeneratorAnnot->initialValue)); } else { if ($tblGeneratorAnnot = $property->getAnnotation('DoctrineTableGenerator')) { throw new DoctrineException("DoctrineTableGenerator not yet implemented."); } } } else { if ($oneToOneAnnot = $property->getAnnotation('DoctrineOneToOne')) { $mapping['targetEntity'] = $oneToOneAnnot->targetEntity; $mapping['joinColumns'] = $joinColumns; $mapping['mappedBy'] = $oneToOneAnnot->mappedBy; $mapping['cascade'] = $oneToOneAnnot->cascade; $metadata->mapOneToOne($mapping); } else { if ($oneToManyAnnot = $property->getAnnotation('DoctrineOneToMany')) { $mapping['mappedBy'] = $oneToManyAnnot->mappedBy; $mapping['targetEntity'] = $oneToManyAnnot->targetEntity; $mapping['cascade'] = $oneToManyAnnot->cascade; $metadata->mapOneToMany($mapping); } else { if ($manyToOneAnnot = $property->getAnnotation('DoctrineManyToOne')) { $mapping['joinColumns'] = $joinColumns; $mapping['cascade'] = $manyToOneAnnot->cascade; $mapping['targetEntity'] = $manyToOneAnnot->targetEntity; $metadata->mapManyToOne($mapping); } else { if ($manyToManyAnnot = $property->getAnnotation('DoctrineManyToMany')) { $joinTable = array(); if ($joinTableAnnot = $property->getAnnotation('DoctrineJoinTable')) { $joinTable = array('name' => $joinTableAnnot->name, 'schema' => $joinTableAnnot->schema, 'catalog' => $joinTableAnnot->catalog, 'joinColumns' => $joinTableAnnot->joinColumns, 'inverseJoinColumns' => $joinTableAnnot->inverseJoinColumns); } $mapping['joinTable'] = $joinTable; $mapping['targetEntity'] = $manyToManyAnnot->targetEntity; $mapping['mappedBy'] = $manyToManyAnnot->mappedBy; $mapping['cascade'] = $manyToManyAnnot->cascade; $metadata->mapManyToMany($mapping); } } } } } } }