如何 运行 一次在 CodeIgniter 中迁移多个文件?
How to run multiple migrate file in CodeIgniter at once?
我正在创建一个迁移,起初 table 似乎没问题,但是当我创建第二个迁移时没有错误,但是 table 没有创建。
我这样命名我的 2 迁移 class:
001_inititial_schema.php
002_create_table_quotation_header.php
第一个包含这个:
class Migration_Initial_Schema extends CI_Migration {
public function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
),
'project_name' => array(
'type' => 'varchar',
'constraint' => 100,
),
'description' => array(
'type' => 'text'
),
'date_created' => array(
'type' => 'datetime',
),
'date_updated' => array(
'type' => 'datetime',
),
'status' => array(
'type' => 'tinyint',
'default' => 1
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('flx_project');
}
public function down() {
$this->dbforge->drop_table('flx_project');
}
}
那么第二个:
class CreateTableQuotationHeader extends CI_Migration {
public function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
),
'project_id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
),
'receiver' => array(
'type' => 'varchar',
'constraint' => 100,
),
'address' => array(
'type' => 'text',
),
'attention' => array(
'type' => 'varchar',
'constraint' => 100
),
'reference_number' => array(
'type' => 'varchar',
'constraint' => 50
),
'date_issued' => array(
'type' => 'date'
)
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('flx_project_header');
}
public function down() {
$this->dbforge->drop_table('flx_project_header');
}
}
然后在我的控制器中:
<?php
class Migrate extends CI_Controller {
public function __construct() {
parent::__construct(0);
$this->load->library('migration');
}
public function index() {
$this->load->helper('template');
if(!$this->migration->current()) {
show_error($this->migration->error_string());
} else {
$data['message'] = 'migrate success';
}
renderPage('common/migrate', $data);
}
}
?>
好的,我解决了我的问题,我所做的是仅将两个迁移都包含在 1 个文件中。但是我不知道这是否是运行具有多个表的迁移的正确方法。
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Initial_Schema extends CI_Migration {
public function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
),
'project_name' => array(
'type' => 'varchar',
'constraint' => 100,
),
'description' => array(
'type' => 'text'
),
'date_created' => array(
'type' => 'datetime',
),
'date_updated' => array(
'type' => 'datetime',
),
'status' => array(
'type' => 'tinyint',
'default' => 1
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('flx_project');
$this->dbforge->add_field(array(
'blog_id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'blog_description' => array(
'type' => 'TEXT',
'null' => TRUE,
),
));
$this->dbforge->add_key('blog_id', TRUE);
$this->dbforge->create_table('blog');
}
public function down() {
$this->dbforge->drop_table('flx_project');
$this->dbforge->drop_table('blog');
}
}
?>
我正在创建一个迁移,起初 table 似乎没问题,但是当我创建第二个迁移时没有错误,但是 table 没有创建。
我这样命名我的 2 迁移 class:
001_inititial_schema.php
002_create_table_quotation_header.php
第一个包含这个:
class Migration_Initial_Schema extends CI_Migration {
public function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
),
'project_name' => array(
'type' => 'varchar',
'constraint' => 100,
),
'description' => array(
'type' => 'text'
),
'date_created' => array(
'type' => 'datetime',
),
'date_updated' => array(
'type' => 'datetime',
),
'status' => array(
'type' => 'tinyint',
'default' => 1
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('flx_project');
}
public function down() {
$this->dbforge->drop_table('flx_project');
}
}
那么第二个:
class CreateTableQuotationHeader extends CI_Migration {
public function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
),
'project_id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
),
'receiver' => array(
'type' => 'varchar',
'constraint' => 100,
),
'address' => array(
'type' => 'text',
),
'attention' => array(
'type' => 'varchar',
'constraint' => 100
),
'reference_number' => array(
'type' => 'varchar',
'constraint' => 50
),
'date_issued' => array(
'type' => 'date'
)
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('flx_project_header');
}
public function down() {
$this->dbforge->drop_table('flx_project_header');
}
}
然后在我的控制器中:
<?php
class Migrate extends CI_Controller {
public function __construct() {
parent::__construct(0);
$this->load->library('migration');
}
public function index() {
$this->load->helper('template');
if(!$this->migration->current()) {
show_error($this->migration->error_string());
} else {
$data['message'] = 'migrate success';
}
renderPage('common/migrate', $data);
}
}
?>
好的,我解决了我的问题,我所做的是仅将两个迁移都包含在 1 个文件中。但是我不知道这是否是运行具有多个表的迁移的正确方法。
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Initial_Schema extends CI_Migration {
public function up() {
$this->dbforge->add_field(array(
'id' => array(
'type' => 'int',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
),
'project_name' => array(
'type' => 'varchar',
'constraint' => 100,
),
'description' => array(
'type' => 'text'
),
'date_created' => array(
'type' => 'datetime',
),
'date_updated' => array(
'type' => 'datetime',
),
'status' => array(
'type' => 'tinyint',
'default' => 1
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('flx_project');
$this->dbforge->add_field(array(
'blog_id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'blog_description' => array(
'type' => 'TEXT',
'null' => TRUE,
),
));
$this->dbforge->add_key('blog_id', TRUE);
$this->dbforge->create_table('blog');
}
public function down() {
$this->dbforge->drop_table('flx_project');
$this->dbforge->drop_table('blog');
}
}
?>