yii\db\Migration::createTable PHP Method

createTable() public method

The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), where name stands for a column name which will be properly quoted by the method, and definition stands for the column type which can contain an abstract DB type. The [[QueryBuilder::getColumnType()]] method will be invoked to convert any abstract type into a physical one. If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly put into the generated SQL.
public createTable ( string $table, array $columns, string $options = null )
$table string the name of the table to be created. The name will be properly quoted by the method.
$columns array the columns (name => definition) in the new table.
$options string additional SQL fragment that will be appended to the generated SQL.
    public function createTable($table, $columns, $options = null)
    {
        echo "    > create table {$table} ...";
        $time = microtime(true);
        $this->db->createCommand()->createTable($table, $columns, $options)->execute();
        foreach ($columns as $column => $type) {
            if ($type instanceof ColumnSchemaBuilder && $type->comment !== null) {
                $this->db->createCommand()->addCommentOnColumn($table, $column, $type->comment)->execute();
            }
        }
        echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @inheritdoc
  */
 public function createTable($table, $columns, $options = null)
 {
     if ($options === null && $this->db->driverName === 'mysql') {
         $options = sprintf('CHARACTER SET %s COLLATE %s ENGINE=%s ROW_FORMAT=%s', $this->config['charset'], $this->config['collate'], $this->config['engine'], $this->config['row-format']);
     }
     if (isset($columns['schema']) && is_array($columns['schema'])) {
         parent::createTable($table, $columns['schema'], $options);
         if (isset($columns['pkey'])) {
             $this->addPrimaryKey('pkey', $table, $columns['pkey']);
         }
         if (isset($columns['pkeys'])) {
             foreach ($columns['pkeys'] as $name => $cols) {
                 $this->addPrimaryKey($name, $table, $cols);
             }
         }
         if (isset($columns['indexes'])) {
             foreach ($columns['indexes'] as $name => $cols) {
                 $this->createIndex($name, $table, $cols);
             }
         }
         if (isset($columns['uniques'])) {
             foreach ($columns['uniques'] as $name => $cols) {
                 $this->createIndex($name, $table, $cols, true);
             }
         }
         if (isset($columns['fkeys'])) {
             foreach ($columns['fkeys'] as $name => $config) {
                 $this->addForeignKey($name, $table, $config['from'], $config['to'][0], $config['to'][1], isset($config['delete']) ? $config['delete'] : null, isset($config['update']) ? $config['update'] : null);
             }
         }
     } else {
         parent::createTable($table, $columns, $options);
     }
 }
All Usage Examples Of yii\db\Migration::createTable