【发布时间】:2017-03-14 23:02:21
【问题描述】:
我正在使用 CodeIgniter 3.1.0 开发应用程序。为了改进安装,我编写了一个 Install_Controller 和一个 Install_Model。
我想使用 Database Forge 类来管理数据库架构,但在用户指南中不是很清楚,Google 上没有任何帮助。
我需要使用$this->dbforge->create_database,因为用户对数据库一无所知,所以他要做的就是MySQL“下一步,下一步,安装”,然后运行批处理将 PHP 作为 Web 服务器运行的文件,因此他可以从 Chrome 使用 URL 来安装应用程序。
用户指南说:为了初始化 Forge 类,您的数据库驱动程序必须已经在运行,因为 forge 类依赖它。
所以我已经用用户、密码、数据库名等设置了 config/database.php...即使因为我需要在应用程序中使用它。
当我尝试加载 URL 以安装应用程序时,给我错误:Message: mysqli::real_connect(): (HY000/1049): Unknown database 'test'
那么,如果我需要先拥有 Forge 类,我该如何使用它来创建数据库模式?
一些代码...
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'test',
'dbdriver' => 'mysqli'
);
$autoload['libraries'] = array('database');
class Install extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->model('install_model');
$this->install_model->createDabase();
}
}
class Install_model extends CI_Model {
function __construct() {
parent::__construct();
}
function createDabase() {
$this->load->dbforge();
if ($this->dbforge->create_database('test'))
echo 'Database created!';
else
echo 'Database error!';
}
}
【问题讨论】:
-
我做了一个非常肮脏的解决方法,希望有人给我一个好的答案... 1. 从自动加载器中删除数据库。 2. 使用多个数据库,默认一个相同,但我创建了第二个数据库名称为空的数据库。 3. 使用 Forge 类的第二个数据库,而不是默认数据库。 4.在其他Controller中手动加载默认数据库。
标签: php class codeigniter-3 forge