【问题标题】:CodeIgniter Migration only migrations table createdCodeIgniter Migration 仅迁移表创建
【发布时间】:2018-11-10 07:22:42
【问题描述】:

现在我正在尝试使用迁移到我的 Codeigniter 项目。

我在application/migrationsMigrate.php 上创建了一些迁移文件,作为控制器在我尝试迁移表时创建表。

我将给出迁移文件的示例以及此线程的 Migrate.php 中的内容。

问题发生在我运行迁移时,我使用 cli php index.php migrate current 运行。它显示migration success,但只有migrations 创建的表。有人跟我有同样的问题吗?

Migrate.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Migrate extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        if(! $this->input->is_cli_request()) {
            show_404();
            exit;
        }
        $this->load->library('migration');
    }

    function current()
    {
        if ($this->migration->current()) {
            log_message('error', 'Migration Success.');
            echo "Migration Success";
        } else {
            log_message('error', $this->migration->error_string());
            echo $this->migration->error_string();
        }
    }

    function rollback($version)
    {
        if ($this->migration->version($version)) {
            log_message('error', 'Migration Success.');
            echo "Migration Success";
        } else {
            log_message('error', $this->migration->error_string());
            echo $this->migration->error_string();
        }
    }

    function latest()
    {
        if ($this->migration->latest()) {
            log_message('error', 'Migration Success.');
            echo "Migration Success";
        } else {
            log_message('error', $this->migration->error_string());
            echo $this->migration->error_string();
        }
    }

}

迁移文件示例。

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_Facility_Types extends CI_Migration {

    public function up()
    {
        $this->dbforge->add_field(array(
            'id' => array(
                'type' => 'INT',
                'constraint' => 5,
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            'name' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
            ),
        ));
        $this->dbforge->add_key('id', TRUE);
        $this->dbforge->create_table('facility_types');
    }

    public function down()
    {
        $this->dbforge->drop_table('facility_types');
    }
}

【问题讨论】:

    标签: php database codeigniter migration


    【解决方案1】:

    每次迁移都按数字顺序向前或向后运行,具体取决于所采用的方法。有两种编号样式可供选择:

    顺序:每次迁移按顺序编号,从 001 开始。每个编号必须是三位,并且顺序不能有任何间隙。 (这是 CodeIgniter 3.0 之前的编号方案。)

    时间戳:每个迁移都使用创建迁移时的时间戳进行编号,格式为 YYYYMMDDHHIISS(例如 20121031100537)。这有助于防止在团队环境中工作时出现编号冲突,并且是 CodeIgniter 3.0 及更高版本中的首选方案。 可以使用 application/config/migration.php 文件中的 $config['migration_type'] 设置来选择所需的样式。

    无论您选择使用哪种编号样式,请在迁移文件前加上迁移编号,后跟下划线和迁移的描述性名称。例如:

    001_add_blog.php(顺序编号)

    20121031100537_add_blog.php(时间戳编号)

    根据文档,您必须遵循此文件命名模式,如果您决定使用时间戳,则必须在文件名前加上完整的日期时间格式,然后下划线文件名。

    这就是为什么我更喜欢使用顺序文件命名的原因,因为它更易于使用,进入迁移配置文件并将migration_type 更改为顺序并在migration_version 中给它最后一个文件号。

    假设你有 001_create_sessions.php002_create_users.php003_create_post.php .. 然后在 migration_version 中给它 3 但不要忘记顺序类型。

    【讨论】:

    • 感谢您的回答,它阐明了过程,我通常在 laravel 上使用迁移,这是我第一次在 codeigniter 中使用,谢谢 Sherif
    • 欢迎您,如果这解决了您的问题,请接受此答案。
    • 只是想问一下,对于您在 CodeIgniter 中创建的每个迁移,迁移版本是否也会更新?
    • @aries - 是的,迁移版本也需要更新。
    猜你喜欢
    • 2014-12-01
    • 2013-10-08
    • 2022-12-24
    • 2018-02-01
    • 2023-03-09
    • 1970-01-01
    • 2017-09-23
    • 2013-09-26
    • 2017-07-02
    相关资源
    最近更新 更多