【问题标题】:Where should I Initializing the Forge Class in CodeIgniter and use the create_database function?我应该在哪里初始化 CodeIgniter 中的 Forge 类并使用 create_database 函数?
【发布时间】:2015-01-19 20:56:43
【问题描述】:

我正在阅读关于 CodeIgniterForge 类this 文档,我喜欢使用这个类创建新数据库的想法,这很棒我需要的。 我需要的是使用这个 create_database

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

但我想知道 Forge 类 的代码应该去哪里?它应该在其中一个控制器中吗?它有更好的地方吗?这样做的最佳实践在哪里?如果是的话,它应该在库中还是在迁移文件夹中? 向上功能? 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');
    }
}

【问题讨论】:

    标签: php codeigniter database-migration


    【解决方案1】:

    您在网上的所有需求都尝试用谷歌搜索:)

    您需要在这个新目录中创建一个文件,并且命名格式必须以您的版本号开头,然后是您的类名(使其描述您所做的更改,即您的第一个可能是:初始模式)。我的第一个迁移文件被称为:001_initial_schema.php,我的下一个可能是 002_add_cmets。 您的文件将如下所示:

    <?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)。当您升级到/超过此版本时运行 up 功能,同样,如果您将数据库(使用配置文件)降级到此版本以下,则运行 down 功能。您的向下功能将始终与您的向上功能相反。基本上将您的数据库恢复到添加该版本之前的状态。另请注意,上面的类名与我之前给出的建议文件名相匹配(迁移一词除外),代码期望类以这种方式命名,没有它就无法工作。

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

    我注意到的唯一警告是,当您希望文本或 varchar 字段具有空默认值时。通常在 phpMyAdmin 中,您希望勾选 Null 字段并将默认值设置为 Null。但是,您在这里需要做的就是将 null 指定为 TRUE,您应该可以从我上面的示例中看到这一点。如果您尝试将默认值定义为 null,那么它实际上会将默认值设置为空白字符串。我认为这个问题最终会得到解决,但只要你牢记这一点,那么它就不难避免。

    加入普拉

    【讨论】:

    • create_database 函数去哪儿了?这就是我想知道的
    • “你需要在这个新目录中创建一个文件”阅读上面的链接
    • 在应用程序目录中创建一个名为 migrations 的目录。
    • 链接没有说明 $this->dbforge->create_database('db_name') 函数
    • stackoverflow.com/questions/16500733/… 我认为 $this->dbforge->create_database('db_name') 可以在加载 load->library('migration');和配置 $config['migration_enabled'] = TRUE;
    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 2016-04-06
    • 2016-03-26
    • 2021-01-29
    • 2011-11-04
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    相关资源
    最近更新 更多