Пример #1
0
 public function testConfigMaxIdentifierLength()
 {
     $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
     $schemaConfig->setMaxIdentifierLength(5);
     $schema = new Schema(array(), array(), $schemaConfig);
     $table = $schema->createTable("smalltable");
     $table->addColumn('long_id', 'integer');
     $table->addIndex(array('long_id'));
     $index = current($table->getIndexes());
     $this->assertEquals(5, strlen($index->getName()));
 }
Пример #2
0
 /**
  * From a given set of metadata classes this method creates a Schema instance.
  *
  * @param array $classes
  * @return Schema
  */
 public function getSchemaFromMetadata(array $classes)
 {
     $processedClasses = array();
     // Reminder for processed classes, used for hierarchies
     $metadataSchemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
     $metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
     $metadataSchemaConfig->setMaxIdentifierLength(63);
     $sm = $this->_em->getConnection()->getSchemaManager();
     $schema = new \Doctrine\DBAL\Schema\Schema(array(), array(), $metadataSchemaConfig);
     foreach ($classes as $class) {
         if (isset($processedClasses[$class->name]) || $class->isMappedSuperclass) {
             continue;
         }
         $table = $schema->createTable($class->getQuotedTableName($this->_platform));
         if ($class->isIdGeneratorIdentity()) {
             $table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);
         } else {
             if ($class->isIdGeneratorSequence()) {
                 $table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_SEQUENCE);
             }
         }
         $columns = array();
         // table columns
         if ($class->isInheritanceTypeSingleTable()) {
             $columns = $this->_gatherColumns($class, $table);
             $this->_gatherRelationsSql($class, $table, $schema);
             // Add the discriminator column
             $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
             // Aggregate all the information from all classes in the hierarchy
             foreach ($class->parentClasses as $parentClassName) {
                 // Parent class information is already contained in this class
                 $processedClasses[$parentClassName] = true;
             }
             foreach ($class->subClasses as $subClassName) {
                 $subClass = $this->_em->getClassMetadata($subClassName);
                 $this->_gatherColumns($subClass, $table);
                 $this->_gatherRelationsSql($subClass, $table, $schema);
                 $processedClasses[$subClassName] = true;
             }
         } else {
             if ($class->isInheritanceTypeJoined()) {
                 // Add all non-inherited fields as columns
                 $pkColumns = array();
                 foreach ($class->fieldMappings as $fieldName => $mapping) {
                     if (!isset($mapping['inherited'])) {
                         $columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
                         $this->_gatherColumn($class, $mapping, $table);
                         if ($class->isIdentifier($fieldName)) {
                             $pkColumns[] = $columnName;
                         }
                     }
                 }
                 $this->_gatherRelationsSql($class, $table, $schema);
                 // Add the discriminator column only to the root table
                 if ($class->name == $class->rootEntityName) {
                     $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
                 } else {
                     // Add an ID FK column to child tables
                     /* @var Doctrine\ORM\Mapping\ClassMetadata $class */
                     $idMapping = $class->fieldMappings[$class->identifier[0]];
                     $this->_gatherColumn($class, $idMapping, $table);
                     $columnName = $class->getQuotedColumnName($class->identifier[0], $this->_platform);
                     $pkColumns[] = $columnName;
                     if ($table->isIdGeneratorIdentity()) {
                         $table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_NONE);
                     }
                     // Add a FK constraint on the ID column
                     $table->addUnnamedForeignKeyConstraint($this->_em->getClassMetadata($class->rootEntityName)->getQuotedTableName($this->_platform), array($columnName), array($columnName), array('onDelete' => 'CASCADE'));
                 }
                 $table->setPrimaryKey($pkColumns);
             } else {
                 if ($class->isInheritanceTypeTablePerClass()) {
                     throw DoctrineException::notSupported();
                 } else {
                     $this->_gatherColumns($class, $table);
                     $this->_gatherRelationsSql($class, $table, $schema);
                 }
             }
         }
         if (isset($class->primaryTable['indexes'])) {
             foreach ($class->primaryTable['indexes'] as $indexName => $indexData) {
                 $table->addIndex($indexData['columns'], $indexName);
             }
         }
         if (isset($class->primaryTable['uniqueConstraints'])) {
             foreach ($class->primaryTable['uniqueConstraints'] as $indexName => $indexData) {
                 $table->addUniqueIndex($indexData['columns'], $indexName);
             }
         }
         $processedClasses[$class->name] = true;
         if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
             $seqDef = $class->getSequenceGeneratorDefinition();
             if (!$schema->hasSequence($seqDef['sequenceName'])) {
                 $schema->createSequence($seqDef['sequenceName'], $seqDef['allocationSize'], $seqDef['initialValue']);
             }
         }
     }
     return $schema;
 }
Пример #3
0
 public function testConfigMaxIdentifierLength()
 {
     $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
     $schemaConfig->setMaxIdentifierLength(10);
     $schema = new Schema(array(), array(), $schemaConfig);
     $table = $schema->createTable("smalltable");
     $table->addColumn('long_id', 'integer');
     $table->addIndex(array('long_id'));
     $this->assertTrue($table->hasIndex('le_id_idx'));
 }