Php 已创建CodeIgniter仅迁移迁移表
现在我正试图将迁移应用到我的Codeigniter项目中 我在Php 已创建CodeIgniter仅迁移迁移表,php,database,codeigniter,migration,Php,Database,Codeigniter,Migration,现在我正试图将迁移应用到我的Codeigniter项目中 我在application/migrations和Migrate.php上创建了一些迁移文件,作为尝试迁移表时生成表的控制器 我将给出迁移文件的示例以及此线程的和处的Migrate.php中的内容 当我运行迁移时会出现问题,我使用cliphp index.php migrate current运行迁移。它显示迁移成功,但仅显示创建的迁移表。有人和我有同样的问题吗 Migrate.php <?php if (!defined('BAS
application/migrations
和Migrate.php
上创建了一些迁移文件,作为尝试迁移表时生成表的控制器
我将给出迁移文件的示例以及此线程的和处的Migrate.php
中的内容
当我运行迁移时会出现问题,我使用cliphp index.php migrate current
运行迁移。它显示迁移成功
,但仅显示创建的迁移
表。有人和我有同样的问题吗
Migrate.php
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Migrate extends CI_Controller {
function __construct()
{
parent::__construct();
if(! $this->input->is_cli_request()) {
show_404();
exit;
}
$this->load->library('migration');
}
function current()
{
if ($this->migration->current()) {
log_message('error', 'Migration Success.');
echo "Migration Success";
} else {
log_message('error', $this->migration->error_string());
echo $this->migration->error_string();
}
}
function rollback($version)
{
if ($this->migration->version($version)) {
log_message('error', 'Migration Success.');
echo "Migration Success";
} else {
log_message('error', $this->migration->error_string());
echo $this->migration->error_string();
}
}
function latest()
{
if ($this->migration->latest()) {
log_message('error', 'Migration Success.');
echo "Migration Success";
} else {
log_message('error', $this->migration->error_string());
echo $this->migration->error_string();
}
}
}
根据所采用的方法,每次迁移都以数字顺序向前或向后运行。有两种编号样式可用:
顺序:每次迁移都按顺序编号,从001开始。每个数字必须是三位数字,并且序列中不得有任何间隙。(这是CodeIgniter 3.0之前的编号方案。)
时间戳:每次迁移都使用创建迁移时的时间戳进行编号,格式为YYYYMMDDHIISS(例如20121031100537)。这有助于在团队环境中工作时防止编号冲突,是CodeIgniter 3.0及更高版本中的首选方案。
可以使用应用程序/config/migration.php文件中的$config['migration\u type']设置来选择所需的样式
无论您选择使用哪种编号样式,都应在迁移文件前面加上迁移编号,后跟下划线和迁移的描述性名称。例如:
001\u add\u blog.php(顺序编号)
20121031100537\u add\u blog.php(时间戳编号)
从文档中,您必须遵循此文件命名模式,如果您决定使用时间戳,则必须在文件名前面加上完整的日期时间格式,然后在文件名下面加下划线
这就是为什么我更喜欢使用顺序文件命名,因为它的方式更易于使用,进入migration configs文件并将migration_类型
更改为sequential,在migration_版本
中为其提供最后一个文件号
假设您有001\u create\u sessions.php
,002\u create\u users.php
和003\u create\u post.php
。。然后在migration\u版本中
给它3个,但不要忘记顺序类型。谢谢你的回答,它清楚了这个过程,我通常在laravel上使用migration,这是我第一次在codeigniter中使用,谢谢你,如果这解决了你的问题,请接受这个答案。只是想问一下,对于在CodeIgniter中创建的每次迁移,迁移版本是否也会更新?@aries-是的,迁移版本也需要更新。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Add_Facility_Types extends CI_Migration {
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'name' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('facility_types');
}
public function down()
{
$this->dbforge->drop_table('facility_types');
}
}