【问题标题】:CodeIgniter MigrationCodeIgniter 迁移
【发布时间】: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


    【解决方案1】:

    问题是试图捕获异常,因为dbforge 不会抛出任何异常。您应该恢复到良好的旧 if 条件。

    if($this->dbforge->create_table('users',TRUE,$attributes) === FALSE)
    {
        echo '<pre>Error creating table.</pre>';
        return;
    }
    
    echo '<pre>Table created</pre>';
    $res = $this->db->query("  INSERT INTO `users` VALUES (1, 'SYSTEM', 'NOUSERNAME', '111', 0, NULL, NULL, NULL, -9, NULL, NULL, NULL, 1, NULL, NULL)");
    echo $res ? '<pre>Insert Succeeded</pre>' : '<pre>Insert Failed</pre>';
    

    与您的问题无关,但您不需要Migration_Create_users_table 中的__construct 方法。 dbforge 库由父类 CI_Migration 加载。没有理由覆盖CI_Migration::__construct,如果您确实有理由,调用parent::__construct($config); 将是绝对必要的。 (哦,扩展类需要在其构造函数中接受 $config 参数。)

    如果最后一段不明确(是的,我漫无目的)从Migration_Create_users_table 中删除构造函数。

    【讨论】:

    • 感谢您的提示。我发现了问题:在字段中,user_level 上有一个错字,默认设置为“=9”(无效整数),而它原本是“-9”,而 CI 没有返回 SQL 错误无法创建表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2012-02-27
    • 2015-04-02
    • 2013-02-17
    • 2014-05-05
    相关资源
    最近更新 更多