如何运行CodeIgniter迁移?

问题描述 投票:42回答:5

我知道如何通过q​​azxswpoi创建它们

但是,一旦我创建了我的迁移文件,我该如何运行它们?

codeigniter migration codeigniter-2
5个回答
26
投票

我不确定这是正确的做法,但它对我有用。

我创建了一个名为http://codeigniter.com/user_guide/libraries/migration.html的控制器(controllers / migrate.php)。

migrate

然后从浏览器我将调用此url在<?php defined("BASEPATH") or exit("No direct script access allowed"); class Migrate extends CI_Controller{ public function index($version){ $this->load->library("migration"); if(!$this->migration->version($version)){ show_error($this->migration->error_string()); } } } 控制器中执行index操作 例如:http://localhost/index.php/migrate/index/1


57
投票

使用这些页面作为参考:migrateRunning via the CLI,您可以将对迁移控制器的访问权限限制为命令行(application / controllers / migrate.php):

Migration Class

然后执行最新的迁移,进入项目目录的根目录并运行:

<?php  if ( ! defined('BASEPATH')) exit("No direct script access allowed");

class Migrate extends CI_Controller {

  public function __construct()
  {
    parent::__construct();

    $this->input->is_cli_request() 
      or exit("Execute via command line: php index.php migrate");

    $this->load->library('migration');
  }

  public function index()
  {
    if(!$this->migration->latest()) 
    {
      show_error($this->migration->error_string());
    }
  }
}

但是当您尝试通过webserver domain.com/migrate访问时,您将在上面的脚本中看到该文本。


5
投票

您还可以为向下或向上迁移运行某个版本:

php index.php migrate

对于CLI,运行此命令if(!defined('BASEPATH')) exit('No direct script access allowed'); class Migrate extends CI_Controller{ public function __construct() { parent::__construct(); $this->load->library('migration'); } public function version($version) { if($this->input->is_cli_request()) { $migration = $this->migration->version($version); if(!$migration) { echo $this->migration->error_string(); } else { echo 'Migration(s) done'.PHP_EOL; } } else { show_error('You don\'t have permission for this action');; } } } ,其中php index.php migrate version 5是迁移的版本。如果版本更多是当前迁移 - 向上迁移,否则 - 向下到输入版本。


0
投票

5

试试这个,我已经为此编写了一个库,可以通过CLI轻松使用。它可用于创建迁移文件并向后或向前运行迁移。


0
投票

这是最简单的Codeigniter数据库迁移

  1. 将application / database.php配置为数据库名称设置。
  2. 创建application / config mirate.php`

`.

  1. 在application \ migration.php中更改 <?php defined("BASEPATH") or exit("No direct script access allowed"); class Migrate extends CI_Controller { public function index() { if (ENVIRONMENT == 'development') { $this->load->library('migration'); if ( ! $this->migration->current()) { show_error($this->migration->error_string()); } else { echo "success"; } } else { echo "go away"; } } } ?>
  2. 在文件夹中打开CLI并键入$config['migration_enabled'] = TRUE;
© www.soinside.com 2019 - 2024. All rights reserved.