【发布时间】:2018-11-29 15:29:54
【问题描述】:
我已经开始研究 CodeIgniter 迁移确实提高了我新工作的开发标准。
但是,由于某种原因,我无法在我的数据库上创建任何表。
以上迁移代码:
<php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Create_users_table extends CI_Migration {
public function __construct()
{
parent::__construct();
$this->load->dbforge();
}
public function up()
{
$fields = array(
'user_id' => array(
'type' => 'INT',
'constraint' => 11,
'unique' => TRUE,
'null' => FALSE,
'auto_increment' => TRUE
),
'user_name' => array(
'type' => 'VARCHAR',
'constraint' => '255',
'null' => FALSE,
),
'user_email' => array(
'type' => 'VARCHAR',
'constraint' => '255',
'null' => FALSE,
),
'user_password' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'user_status' => array(
'type' => 'INT',
'constraint' => 1,
'default' => 0
),
'user_permissions' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'device_token' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'user_level' => array(
'type' => 'INT',
'constraint' => 3,
'default' => '=9'
),
'user_photo' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'device_snid' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'user_recovery_token' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'client' => array(
'type' => 'INT',
'constraint' => 11,
'null' => FALSE,
'default' => 0
),
'latitude' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'longitude' => array(
'type' => 'VARCHAR',
'constraint' => '255',
)
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('user_id', TRUE);
$attributes = array('ENGINE' => 'InnoDB');
try{
$this->dbforge->create_table('users',TRUE,$attributes);
echo '<pre>Table created</pre>';
} catch(Exception $e ){
echo '<pre>Error creating table.</pre>';
d($e);
}
$this->db->query(" INSERT INTO `users` VALUES (1, 'SYSTEM', 'NOUSERNAME', '111', 0, NULL, NULL, NULL, -9, NULL, NULL, NULL, 1, NULL, NULL)");
}
public function down()
{
$this->dbforge->drop_table('users',TRUE);
}
}
迁移文件名:/aplication/migrations/20181129120800_Create_users_table.php
运行迁移的控制器方法:
public function do_migration($version = NULL)
{
$this->load->library('migration');
if(isset($version) && ($this->migration->version($version) === FALSE))
{
show_error($this->migration->error_string());
}
elseif(is_null($version) && $this->migration->latest() === FALSE)
{
show_error($this->migration->error_string());
}
else
{
echo 'The migration has concluded successfully.';
}
}
CodeIgniter 会按预期创建“迁移”表并将“版本”设置为文件名的时间戳。
'users' 表没有被创建。打印调试消息“表已创建”。
如果我测试方法 $this->dbforge->create_table('users',TRUE,$attributes); 它返回 false。
尝试调试并尝试/捕获错误,但唯一的结果是 bool(false)。
如果我创建任何表并选择其中的数据,CI 会按预期返回它,因为数据库连接正常。
编辑
找出问题所在:在字段中,user_level 出现了拼写错误,默认设置为“=9”(无效整数),而原本应该是“-9”。
【问题讨论】:
标签: php codeigniter migration