我使用的是适用于CodeIgniter 2的迁移代码,但它们不适用于CodeIgniter 3
我已经阅读了与我的问题相似的大多数问题,但是找不到答案。
........
class Migration_Create_users extends CI_Migration
{
public function up()
{
$this->dbforge->add_field([
'id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true,
],
'email' => [
'type' => 'VARCHAR',
'constraint' => '100',
],
'password' => [
'type' => 'VARCHAR',
'constraint' => '128',
],
'name' => [
'type' => 'VARCHAR',
'constraint' => '100',
],
]);
$this->dbforge->add_key('id', true);
$this->dbforge->create_table('users');
}
public function down()
{
$this->dbforge->drop_table('users');
}
}
class Migration extends Admin_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('migration');
if (!$this->migration->current()) {
show_error($this->migration->error_string());
} else {
echo 'Migration worked!';
}
}
}
//迁移配置
defined('BASEPATH') or exit('No direct script access allowed');
$config['migration_enabled'] = true;
$config['migration_type'] = 'sequential';
$config['migration_table'] = 'migrations';
$config['migration_auto_latest'] = false;
$config['migration_version'] = 001;
$config['migration_path'] = APPPATH . 'migrations/';
我希望运行代码,然后在数据库中创建表
答案 0 :(得分:0)
请使用以下功能
class Migrate extends CI_Controller{
public function index($version){
$this->load->library("migration");
if(!$this->migration->version($version)){
show_error($this->migration->error_string());
}
}
}