/**
  * {@inheritdoc}
  *
  * @todo Table names should be computed in DBAL depending on the platform
  */
 public function getTableName(ClassMetadata $class, AbstractPlatform $platform)
 {
     $tableName = $class->table['name'];
     if (!empty($class->table['schema'])) {
         $tableName = $class->table['schema'] . '.' . $class->table['name'];
         if (!$platform->supportsSchemas() && $platform->canEmulateSchemas()) {
             $tableName = $class->table['schema'] . '__' . $class->table['name'];
         }
     }
     return isset($class->table['quoted']) ? $platform->quoteIdentifier($tableName) : $tableName;
 }
Exemple #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)
 {
     // Reminder for processed classes, used for hierarchies
     $processedClasses = array();
     $eventManager = $this->em->getEventManager();
     $schemaManager = $this->em->getConnection()->getSchemaManager();
     $metadataSchemaConfig = $schemaManager->createSchemaConfig();
     $metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
     $schema = new Schema(array(), array(), $metadataSchemaConfig);
     foreach ($classes as $class) {
         if ($this->processingNotRequired($class, $processedClasses)) {
             continue;
         }
         $table = $schema->createTable($this->quoteStrategy->getTableName($class, $this->platform));
         $columns = array();
         // table columns
         if ($class->isInheritanceTypeSingleTable()) {
             $columns = $this->_gatherColumns($class, $table);
             $this->_gatherRelationsSql($class, $table, $schema);
             // Add the discriminator column
             $this->addDiscriminatorColumnDefinition($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 = $this->quoteStrategy->getColumnName($mapping['fieldName'], $class, $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) {
                     $this->addDiscriminatorColumnDefinition($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 = $this->quoteStrategy->getColumnName($class->identifier[0], $class, $this->platform);
                     // TODO: This seems rather hackish, can we optimize it?
                     $table->getColumn($columnName)->setAutoincrement(false);
                     $pkColumns[] = $columnName;
                     // Add a FK constraint on the ID column
                     $table->addUnnamedForeignKeyConstraint($this->quoteStrategy->getTableName($this->em->getClassMetadata($class->rootEntityName), $this->platform), array($columnName), array($columnName), array('onDelete' => 'CASCADE'));
                 }
                 $table->setPrimaryKey($pkColumns);
             } else {
                 if ($class->isInheritanceTypeTablePerClass()) {
                     throw ORMException::notSupported();
                 } else {
                     $this->_gatherColumns($class, $table);
                     $this->_gatherRelationsSql($class, $table, $schema);
                 }
             }
         }
         $pkColumns = array();
         foreach ($class->identifier as $identifierField) {
             if (isset($class->fieldMappings[$identifierField])) {
                 $pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
             } else {
                 if (isset($class->associationMappings[$identifierField])) {
                     /* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
                     $assoc = $class->associationMappings[$identifierField];
                     foreach ($assoc['joinColumns'] as $joinColumn) {
                         $pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
                     }
                 }
             }
         }
         if (!$table->hasIndex('primary')) {
             $table->setPrimaryKey($pkColumns);
         }
         if (isset($class->table['indexes'])) {
             foreach ($class->table['indexes'] as $indexName => $indexData) {
                 $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName);
             }
         }
         if (isset($class->table['uniqueConstraints'])) {
             foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
                 $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName);
             }
         }
         if (isset($class->table['options'])) {
             foreach ($class->table['options'] as $key => $val) {
                 $table->addOption($key, $val);
             }
         }
         $processedClasses[$class->name] = true;
         if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
             $seqDef = $class->sequenceGeneratorDefinition;
             $quotedName = $this->quoteStrategy->getSequenceName($seqDef, $class, $this->platform);
             if (!$schema->hasSequence($quotedName)) {
                 $schema->createSequence($quotedName, $seqDef['allocationSize'], $seqDef['initialValue']);
             }
         }
         if ($eventManager->hasListeners(ToolEvents::postGenerateSchemaTable)) {
             $eventManager->dispatchEvent(ToolEvents::postGenerateSchemaTable, new GenerateSchemaTableEventArgs($class, $schema, $table));
         }
     }
     if (!$this->platform->supportsSchemas() && !$this->platform->canEmulateSchemas()) {
         $schema->visit(new RemoveNamespacedAssets());
     }
     if ($eventManager->hasListeners(ToolEvents::postGenerateSchema)) {
         $eventManager->dispatchEvent(ToolEvents::postGenerateSchema, new GenerateSchemaEventArgs($this->em, $schema));
     }
     return $schema;
 }
 /**
  * Gets the sequence name prefix based on class metadata.
  *
  * @param AbstractPlatform $platform
  * @return string
  *
  * @todo Sequence names should be computed in DBAL depending on the platform
  */
 public function getSequencePrefix(AbstractPlatform $platform)
 {
     $tableName = $this->getTableName();
     $sequencePrefix = $tableName;
     // Prepend the schema name to the table name if there is one
     if ($schemaName = $this->getSchemaName()) {
         $sequencePrefix = $schemaName . '.' . $tableName;
         if (!$platform->supportsSchemas() && $platform->canEmulateSchemas()) {
             $sequencePrefix = $schemaName . '__' . $tableName;
         }
     }
     return $sequencePrefix;
 }