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.
Exemplo 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);
     }
 }
Exemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function createTable($table, $columns, $options = null)
 {
     if ($options === null && $this->db->driverName === 'mysql') {
         $options = "CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB";
     }
     parent::createTable($table, $columns, $options);
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 public function createTable($table, $columns, $options = null)
 {
     if ($options === null && $this->db->driverName === 'mysql') {
         // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
         $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
     }
     parent::createTable($table, $columns, $options);
 }
Exemplo n.º 4
0
 /**
  * @param Migration $migration
  * @param string $tableName
  */
 public static function migrateUp($migration, $tableName = '{{%files}}')
 {
     $tableOptions = null;
     if ($migration->db->driverName === 'mysql') {
         $tableOptions = 'ENGINE=InnoDB CHARSET=utf8';
     }
     $migration->createTable($tableName, ['id' => Schema::TYPE_PK, 'original_name' => Schema::TYPE_STRING, 'file_size' => Schema::TYPE_INTEGER, 'created_at' => Schema::TYPE_DATETIME, 'updated_at' => Schema::TYPE_DATETIME]);
 }
 /**
  * @inheritdoc
  */
 public function createTable($name, $columns, $options = null)
 {
     $baseTableName = $this->db->getSchema()->getRawTableName($name);
     if ($this->db->getSchema()->getTableSchema($baseTableName, true) !== null) {
         echo "    > table {$name} already exists ...\n";
         return;
     }
     parent::createTable($name, $columns, $options);
 }
Exemplo n.º 6
0
 public function install()
 {
     parent::install();
     $migration = new Migration();
     $migration->createTable($this->getTable(), ['id' => Schema::TYPE_PK, 'object_id' => Schema::TYPE_INTEGER, 'term_id' => Schema::TYPE_BIGINT, 'value' => Schema::TYPE_STRING]);
     if ($migration->db->driverName === 'mysql') {
         $migration->addForeignKey('fk_' . $this->getTable() . '_' . $this->getRefTableName(), $this->getTable(), 'object_id', $this->getRefTableName(), 'id', 'CASCADE');
         $migration->addForeignKey('fk_' . $this->getTable() . '_' . TaxonomyTerms::tableName(), $this->getTable(), 'term_id', TaxonomyTerms::tableName(), 'id', 'CASCADE');
     }
 }
Exemplo n.º 7
0
 /**
  * @inheritdoc
  */
 public function createTable($table, $columns, $options = null)
 {
     if (is_null($options) && in_array($this->getDb()->getDriverName(), ['mysql', 'mysqli'])) {
         // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
         $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
     }
     foreach ($columns as $name => $type) {
         $columns[$name] = $this->fixColumnType($type);
     }
     parent::createTable($table, $columns, $options);
 }
 /**
  * Create table in database
  */
 public function runCreateTable()
 {
     $this->dropTable();
     if ($this->hideMigrationOutput) {
         ob_start();
     }
     /** @var Connection $_conn */
     $_conn = Yii::$app->{$this->db};
     if (!$_conn->schema->getTableSchema($this->tableName)) {
         $this->migrationClass->createTable($this->tableNameRaw, $this->columns);
         if (is_array($this->primaryKeys) && sizeof($this->primaryKeys)) {
             try {
                 $this->migrationClass->addPrimaryKey("{$this->tableNameRaw}_pk", $this->tableNameRaw, $this->primaryKeys);
             } catch (\yii\db\Exception $exp) {
             }
         }
     }
     if ($this->hideMigrationOutput) {
         ob_clean();
         ob_flush();
     }
 }
Exemplo n.º 9
0
 function actionTableusercompany()
 {
     $id_company = "FIELD1";
     $company_name = "FIELD2";
     $company_logo = "FIELD3";
     $company_color = "FIELD4";
     $company_image = "FIELD5";
     $migration = new Migration();
     $query = $migration->createTable('TABLE34', [$id_company => 'pk', $company_name => 'string', $company_logo => 'string', $company_color => 'string', $company_image => 'string']);
 }
Exemplo n.º 10
0
 /**
  * @param bool $insert
  * @param array $changedAttributes
  * @return bool
  */
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     if ($insert) {
         $migrate = new Migration();
         $tableOptions = '';
         if ($migrate->db->driverName === 'mysql') {
             $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
         }
         $table = self::getTable($this->tab_index, $this->tab);
         $tableOptions = $tableOptions . " COMMENT '" . Yii::t('app', 'Comment prefix') . $this->name . "'";
         $migrate->createTable($table, ['id' => $migrate->primaryKey()], $tableOptions);
         return true;
     }
 }
Exemplo n.º 11
0
 /**
  * @inheritdoc
  * Notes:
  *  - table will be auto pefixied if [[$autoWrapTableNames]] is true.
  *  - options will be auto set by character set and collate for mysql db.
  */
 public function createTable($table, $columns, $options = null)
 {
     if ($options === null && $this->db->driverName === 'mysql') {
         // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
         $charset = $this->db->charset;
         $collate = $charset . '_unicode_ci';
         $options = "CHARACTER SET {$charset} COLLATE {$collate} ENGINE=InnoDB";
     }
     $table = $this->autoWrappedTableName($table);
     return parent::createTable($table, $columns, $options);
 }
Exemplo n.º 12
0
 /**
  * @inheritdoc
  */
 public function createTable($table, $columns, $options = null)
 {
     parent::createTable($table, $columns, $options === null ? $this->getTableOptions() : $options);
 }