如何在CodeIgniter中一次运行多个迁移文件?

时间:2015-05-26 02:46:26

标签: php database codeigniter migration

我正在创建一个迁移,第一个表似乎没问题,但是当我创建第二个迁移时,没有错误但是没有创建表。

我将我的2迁移类命名为:

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 个答案:

答案 0 :(得分:2)

好的我解决了我的问题我所做的是我只将迁移包含在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');
    }


}

?>