现在我正在尝试使用迁移到我的 Codeigniter 项目。
我在 上创建了一些迁移文件application/migrations
,并Migrate.php
作为控制器在我尝试迁移表时创建表。
我将提供迁移文件的示例Migrate.php
以及该线程中的内容。
当我运行迁移时出现问题,我使用 cli 运行php index.php migrate current
。它显示migration success
,但仅migrations
创建了表。有人和我有同样的问题吗?
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();
}
}
}
迁移文件示例。
<?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');
}
}
根据采用的方法,每个迁移按数字顺序向前或向后运行。有两种编号样式可用:
Sequential:每次迁移都是按顺序编号的,从001开始,每个编号必须是三位数,并且顺序不能有任何间隙。(这是 CodeIgniter 3.0 之前的编号方案。)
Timestamp:每次迁移都使用创建迁移时的时间戳进行编号,格式为 YYYYMMDDHHIISS(例如 20121031100537)。这有助于在团队环境中工作时防止编号冲突,并且是 CodeIgniter 3.0 及更高版本中的首选方案。可以使用 application/config/migration.php 文件中的 $config['migration_type'] 设置来选择所需的样式。
无论您选择使用哪种编号样式,请使用迁移编号作为迁移文件的前缀,后跟下划线和迁移的描述性名称。例如:
001_add_blog.php(顺序编号)
20121031100537_add_blog.php(时间戳编号)
从文档中,您必须遵循此文件命名模式,如果您决定使用时间戳,则必须在文件名之前使用完整的日期时间格式,然后下划线文件名。
这就是为什么我更喜欢使用顺序文件命名的原因,因为它更易于使用,进入迁移配置文件并将其更改migration_type
为顺序并migration_version
为其提供最后一个文件编号。
假设您有001_create_sessions.php
,002_create_users.php
和003_create_post.php
.. 然后在migration_version
给它 3 但不要忘记顺序类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句