dpy83214 2015-05-26 05:42
浏览 54
已采纳

如何在CodeIgniter的迁移中使用down()方法?

I read the CodeIgniter's documentation about the migration class. I am able to perform the migration but how can I use the down() function in the migration?

Here's the sample from the documentation

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

class Migration_Add_blog extends CI_Migration {

    public function up()
    {
        $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->create_table('blog');
    }

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

In my page I setup the migration like this:

class Migrate extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $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);

    }

}

How can I call the down() method?

  • 写回答

1条回答 默认 最新

  • douxun1934 2015-05-26 19:57
    关注

    tl;dr

    Think of Migrations as versions in Git.

    Invoke down() method by invoking older migration than current.

    ts;dr

    Lets say you have 3 migration files as per current time under your directory application/migrations/.

    1. 001_add_clients.php
    2. 002_add_customers.php
    3. 003_add_blog.php

    So, as per now your application has passed through 3 migrations as listed above and your current status of migration in your config/migration.php is set to

    $config['migration_table'] = 'migrations';
    $config['migration_version'] = 3;
    

    Your table named migrations has version set to 3.

    Now, when you run $this->migration->current(); through your migration controller, it will do nothing because your are already in current state of migration.


    Now, somehow your idea of creating blog failed and you want your application to return in migration state 002.

    You invoke this by calling $this->migration->version(2); in your controller

    or

    setting $config['migration_version'] = 2; in your config.php and $this->migration->current(); in your controller.

    This is the scenario where

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

    method invokes of file 003_add_blog.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划