Doctrine\ORM\Tools\SchemaTool::getDropSchemaSQL PHP Method

getDropSchemaSQL() public method

public getDropSchemaSQL ( array $classes ) : array
$classes array
return array
    public function getDropSchemaSQL(array $classes)
    {
        $sm = $this->_em->getConnection()->getSchemaManager();
        
        $sql = array();
        $orderedTables = array();

        foreach ($classes AS $class) {
            if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName && $this->_platform->supportsSequences()) {
                $sql[] = $this->_platform->getDropSequenceSQL($class->sequenceGeneratorDefinition['sequenceName']);
            }
        }

        $commitOrder = $this->_getCommitOrder($classes);
        $associationTables = $this->_getAssociationTables($commitOrder);

        // Drop association tables first
        foreach ($associationTables as $associationTable) {
            if (!in_array($associationTable, $orderedTables)) {
                $orderedTables[] = $associationTable;
            }
        }

        // Drop tables in reverse commit order
        for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
            $class = $commitOrder[$i];

            if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
                || $class->isMappedSuperclass) {
                continue;
            }

            if (!in_array($class->getTableName(), $orderedTables)) {
                $orderedTables[] = $class->getTableName();
            }
        }

        $dropTablesSql = array();
        foreach ($orderedTables AS $tableName) {
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
            $foreignKeys = $sm->listTableForeignKeys($tableName);
            foreach ($foreignKeys AS $foreignKey) {
                $sql[] = $this->_platform->getDropForeignKeySQL($foreignKey, $tableName);
            }
            $dropTablesSql[] = $this->_platform->getDropTableSQL($tableName);
        }

        return array_merge($sql, $dropTablesSql);
    }

Usage Example

Example #1
0
 protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
 {
     $isFullDatabaseDrop = $input->getOption('full-database');
     if ($input->getOption('dump-sql') === true) {
         if ($isFullDatabaseDrop) {
             $sqls = $schemaTool->getDropDatabaseSQL();
         } else {
             $sqls = $schemaTool->getDropSchemaSQL($metadatas);
         }
         $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL);
     } else {
         if ($input->getOption('force') === true) {
             $output->write('Dropping database schema...' . PHP_EOL);
             if ($isFullDatabaseDrop) {
                 $schemaTool->dropDatabase();
             } else {
                 $schemaTool->dropSchema($metadatas);
             }
             $output->write('Database schema dropped successfully!' . PHP_EOL);
         } else {
             $output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL . PHP_EOL);
             if ($isFullDatabaseDrop) {
                 $sqls = $schemaTool->getDropDatabaseSQL();
             } else {
                 $sqls = $schemaTool->getDropSchemaSQL($metadatas);
             }
             if (count($sqls)) {
                 $output->write('Schema-Tool would execute ' . count($sqls) . ' queries to drop the database.' . PHP_EOL);
                 $output->write('Please run the operation with --force to execute these queries or use --dump-sql to see them.' . PHP_EOL);
             } else {
                 $output->write('Nothing to drop. The database is empty!' . PHP_EOL);
             }
         }
     }
 }
All Usage Examples Of Doctrine\ORM\Tools\SchemaTool::getDropSchemaSQL