问题描述
我正在尝试将重置功能添加到codeigniter的迁移中。下面是我的代码:
I am trying to add reset function to codeigniter's migration. Below is my code:
class Migration extends Backend_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('migration');
}
public function index()
{
//...
}
public function reset()
{
$this->migration->version(1);
$this->db->truncate('ci_sessions');
$this->migration->current();
}
}
返回错误:
Fatal error: Cannot redeclare class Migration_Create_geo_data in D:\web_projects\vProject\framework\application\migrations\002_create_geo_data.php on line 44
如果我单独运行它们,一切都很好。在一起时会出错。知道吗?
If I run them seperately, all are okay. When together it gives error. Any idea?
推荐答案
最有可能,此错误是由于将迁移设置为如果表不存在而创建的,并且
Most likely, this error is a result of setting your migrations to create tables if they do not already exist AND cache data not being updated right away.
您的迁移脚本调用方法,该方法具有两个参数。参数一是表名,参数二是可选的。这是 if_not_exists
标志。但是,默认值为false。如果将其设置为true,则仅在不存在的表中创建表。
Your Migration script calls the DB_forge::create_table
method which takes two parameters. Parameter one is the table name and parameter two is optional. It is the if_not_exists
flag. The default value is false however; if it is set to true tables will only be created if they do not already exist.
如果您的表是使用 if_not_exists 参数设置为 false ,缓存问题将(可能)永远不会发生:
If your tables are created with the
if_not_exists
parameter set to false the caching issue will (probably) never happen:
$this->dbforge->create_table('table_name');
如果使用
if_not_exists
参数创建表设置为 true ,则需要在重新加载迁移时强制更新缓存。
If tables are created with the
if_not_exists
parameter set to true, you will need to force the cache to update when reloading your migrations.
$this->dbforge->create_table('table_name', TRUE);
以下是避免此问题的几种选择:
Here are a couple options to avoid this issue:
- 仅将表名作为参数发送到
create_table
方法
- 在
migration-> version(0) data_cache ['table_names']
c>调用
- Only send the table name as a parameter to the
create_table
method - Unset
data_cache['table_names']
after the migration->version(0)
call
如果选择选项2,则此方法有效:
If you choose option 2, here is a method that works:
public function reset() {
$this->load->library('migration');
if (!$this->migration->version(0)) {
echo $this->migration->error_string();
}
// unset table cache - this will force it to update
unset($this->db->data_cache['table_names']);
if (!$this->migration->current()) {
echo $this->migration->error_string();
}
}
除此之外,迁移文件会自动加载并保存在会话中。
我在system / libraries / Migration.php中更改了这一行: include $ f [0];
改为 include_once $ f [0] ;
这篇关于Codeigniter迁移无法重新声明类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!