gpt4 book ai didi

php - 我应该在哪里初始化 CodeIgniter 中的 Forge 类并使用 create_database 函数?

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:11 24 4
gpt4 key购买 nike

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

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

但是我想知道Forge Class的代码到底应该放在哪里呢?它应该在其中一个 Controller 中吗?它有更好的地方吗?这样做的最佳做法在哪里?它应该在库中还是在迁移文件夹中,如果有什么功能? up 功能? 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');
}
}

最佳答案

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

您需要在这个新目录中创建一个文件,命名格式必须以您的版本号开头,后跟您的类名(使其描述您所做的更改,即您的第一个可能是:初始模式)。我的第一个迁移文件名为: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)。当您升级到/超过此版本时运行 up 函数,同样,如果您将数据库降级(使用配置文件)低于此版本,则运行 down 函数。您的向下功能将始终与您的向上功能相反。基本上将您的数据库恢复到添加该版本之前的状态。另请注意,上面类的名称与我之前提供的建议文件名相匹配(Migration 一词除外),代码期望类以这种方式命名,没有它就无法工作。

我不会详细介绍在创建或修改表时用于字段数组的格式,因为 CodeIgniter 的数据库 Forge 类用户指南已经对此进行了完美的解释。

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

投入Pra

关于php - 我应该在哪里初始化 CodeIgniter 中的 Forge 类并使用 create_database 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27065999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com