我应该在哪里初始化Code​​Igniter中的Forge类并使用create_database函数?

时间:2014-11-21 16:30:43

标签: php codeigniter database-migration

我正在阅读有关 CodeIgniter Forge Class this文档,我喜欢使用此类创建新数据库的想法,即我的需要很棒。 我需要的是使用 create_database

$this->dbforge->create_database('db_name')

但我想知道 Forge Class 的代码应该在哪里?它应该在其中一个控制器中吗?它有更好的位置吗?这样做的最佳做法是哪里?它应该在库中还是在迁移文件夹中,如果是这样的话? 向上功能? CodeIgniter没有在文档中指定!

这是我到目前为止添加了创建表的代码,但 create_database 函数在哪里?:

<?php

class Migration_Users extends CI_Migration {
    public function up(){
        $this->dbforge->add_field("`id` int(11) NOT NULL AUTO_INCREMENT");
        $this->dbforge->add_field("`role_id` int(11) NOT NULL DEFAULT '2'");
        $this->dbforge->add_field("`username` varchar(25) COLLATE utf8_bin NOT NULL UNIQUE");
        $this->dbforge->add_field("`password` varchar(1024) COLLATE utf8_bin NOT NULL");
        $this->dbforge->add_field("`email` varchar(100) COLLATE utf8_bin NOT NULL UNIQUE");
        $this->dbforge->add_field("`banned` tinyint(1) NOT NULL DEFAULT '0'");
        $this->dbforge->add_field("`ban_reason` varchar(255) COLLATE utf8_bin DEFAULT NULL");
        $this->dbforge->add_field("`newpass` varchar(34) COLLATE utf8_bin DEFAULT NULL");
        $this->dbforge->add_field("`newpass_key` varchar(32) COLLATE utf8_bin DEFAULT NULL");
        $this->dbforge->add_field("`newpass_time` datetime DEFAULT NULL");
        $this->dbforge->add_field("`last_ip` varchar(40) COLLATE utf8_bin NOT NULL");
        $this->dbforge->add_field("`last_login` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'");
        $this->dbforge->add_field("`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'");
        $this->dbforge->add_field("`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
        $this->dbforge->add_field("`deleted` tinyint(1) NOT NULL DEFAULT '0'");

        $this->dbforge->add_key('id', TRUE);

        $this->dbforge->create_table('users', TRUE);
    }

    public function down(){
        $this->dbforge->drop_table('users');
    }
}

1 个答案:

答案 0 :(得分:0)

你在网上的所有需求都试着谷歌吧:)

您需要在此新目录中创建一个文件,并且命名格式必须以您的版本号开头,后跟您的类的名称(使其描述您正在进行的更改,即您的第一个可能是: initial_schema)。我的第一个迁移文件名为:001_initial_schema.php,我的下一个文件可能是002_add_comments。 您的文件看起来像这样:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Initial_schema extends CI_Migration {

    public function up()
    {
        $this->dbforge->add_field(array(
            'postid' => array(
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => TRUE,
                'auto_increment' => TRUE
            ),
            'title' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
            ),
            'content' => array(
                'type' => 'TEXT',
                'null' => TRUE,
            ),
        ));

        $this->dbforge->create_table('posts');
    }

    public function down()
    {
        $this->dbforge->drop_table('posts');
    }
}

您会注意到此课程中有两个功能(向上和向下)。升级到/超过此版本时会运行up功能,如果您在此版本下降级数据库(使用配置文件),则会运行down功能。你的向下功能总是与你的向上功能相反。基本上将数据库恢复到添加该版本之前的状态。还要注意上面类的名称与我之前给出的建议文件名匹配(除了单词Migration),代码期望以这种方式命名类,如果没有它,它将无法工作。

我不会详细介绍在创建或修改表时用于字段数组的格式,因为CodeIgniter的Database Forge类用户指南已经完美地解释了这一点。

我注意到的唯一警告是你想要一个带有null默认值的text或varchar字段。通常在phpMyAdmin中,您可以勾选Null字段并将默认值设置为Null。但是,您需要在此处执行的操作是将null指定为TRUE,您应该可以从上面的示例中看到它。如果您尝试将default定义为null,则它实际上将默认值设置为空字符串。我认为这个问题最终会被清除,但只要你记住它就不难避免。

投入Pra