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

getUpdateSchemaSql() public method

Gets the sequence of SQL statements that need to be performed in order to bring the given class mappings in-synch with the relational schema.
public getUpdateSchemaSql ( array $classes, $saveMode = false ) : array
$classes array The classes to consider.
return array The sequence of SQL statements.
    public function getUpdateSchemaSql(array $classes, $saveMode=false)
    {
        $sm = $this->_em->getConnection()->getSchemaManager();

        $fromSchema = $sm->createSchema();
        $toSchema = $this->getSchemaFromMetadata($classes);

        $comparator = new \Doctrine\DBAL\Schema\Comparator();
        $schemaDiff = $comparator->compare($fromSchema, $toSchema);

        if ($saveMode) {
            return $schemaDiff->toSaveSql($this->_platform);
        } else {
            return $schemaDiff->toSql($this->_platform);
        }
    }

Usage Example

Example #1
0
 protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
 {
     // Defining if update is complete or not (--complete not defined means $saveMode = true)
     $saveMode = $input->getOption('complete') !== true;
     if ($input->getOption('dump-sql') === true) {
         $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode);
         $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL);
     } else {
         if ($input->getOption('force') === true) {
             $output->write('Updating database schema...' . PHP_EOL);
             $schemaTool->updateSchema($metadatas, $saveMode);
             $output->write('Database schema updated successfully!' . PHP_EOL);
         } else {
             $output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL);
             $output->write('Use the incremental update to detect changes during development and use' . PHP_EOL);
             $output->write('this SQL DDL to manually update your database in production.' . PHP_EOL . PHP_EOL);
             $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode);
             if (count($sqls)) {
                 $output->write('Schema-Tool would execute ' . count($sqls) . ' queries to update 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 update. The database is in sync with the current entity metadata.' . PHP_EOL);
             }
         }
     }
 }
All Usage Examples Of Doctrine\ORM\Tools\SchemaTool::getUpdateSchemaSql