如何运行CodeIgniter迁移?

kx1ctssn  于 2022-12-06  发布在  其他
关注(0)|答案(8)|浏览(131)

我知道如何通过http://codeigniter.com/user_guide/libraries/migration.html创建它们
但是,一旦我创建了迁移文件,如何运行它们呢?

d4so4syb

d4so4syb1#

使用以下页面作为参考:Running via the CLIMigration Class,您可以沿着以下命令行(application/controllers/migrate.php)将对迁移控制器的访问限制为命令行:

<?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());
    }
  }
}

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

php index.php migrate

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

dphi5xsq

dphi5xsq2#

我不确定这是不是正确的方法,但它对我很有效。
我创建了一个名为migrate**(controllers/migrate.php)**的控制器。

<?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());
      }   
    }
}

然后从浏览器中调用此url以在migrate控制器中执行index操作
例如:http://本地主机/索引.php/迁移/索引/1

mqxuamgl

mqxuamgl3#

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

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');;
        }
     }
 }

对于CLI,运行此命令php index.php migrate version 5,其中5是迁移的版本。如果版本高于当前迁移,则向上迁移,否则向下迁移至输入的版本。

wz8daaqr

wz8daaqr4#

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

1.将application/database.php配置为您的数据库名称设置。
1.创建应用程序/配置mirate.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";
        }
    }
}

1.在应用程序\migration.php中,将$config['migration_enabled'] = TRUE;更改为。
1.在文件夹中打开CLI并键入php index.php migrate

kuuvgm7e

kuuvgm7e5#

我想我有一个最简单的解决方案。(这是为Codeigniter 3. 1. 11)
上述解决方案建议通过url或cli进行迁移。
第一个问题是你把这个应该是幕后的问题公之于众。
第二个问题是,如果你在共享主机平台上部署这个项目,你就没有机会通过cli运行它。
因此,最简单的解决方案是让codeigniter为我们做这项工作:(假设您已正确完成数据库设置和创建迁移)
1.在/application/config/migration. php中创建一个新的文件夹。
1.如下定义迁移版本$config['migration_version'] = 20201019123900;
1.设置$config['migration_auto_latest'] = TRUE;
1.自动加载或手动加载迁移库(在/application/config/autoload. php中定义,如$autoload['libraries'] = array('migration');在控制器构造函数中加载,如$this->load->library('migration');
1.运行您的应用程序(通过浏览器打开)
Tata!好了。你可以检查一下你的数据库,看看你的表是否创建正确。
开发环境应该可以保留这样的设置。
但是对于生产环境,我们应该重置$config['migration_enabled'] = FALSE;,正如注解部分所指出的。

  • 此解决方案可能会受到批评,因为每次运行应用程序或控制器时都会加载迁移库,但我并没有说它是最佳解决方案,我只是说它是最简单的解决方案。*
uhry853o

uhry853o6#

在应用程序\迁移. php中,改变$config ['迁移_已启用']= TRUE;文件
您说的是应用程序\迁移. php,实际上是应用程序\配置\迁移. php。

gudnpqoy

gudnpqoy7#

<?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";
        }
    }enter code here
}
jhiyze9q

jhiyze9q8#

A CI第4版答案(2022年):

在版本4中,它略有不同:https://codeigniter.com/user_guide/dbmgmt/migration.html

namespace App\Controllers;

use CodeIgniter\Controller;
use Throwable;

class Migrate extends Controller {

    public function index(){
        $migrate = \Config\Services::migrations();

        try {
            $migrate->latest();
            $migrate->regress();

            echo 'Migration complete';
        } catch (Throwable $e) {
            print'<pre>';print_r($e);print'</pre>';
        }
    }
}

注意,如果您也希望回滚迁移,我已经包括了回归选项。

相关问题