[英]Codeigniter 3 - How to use Forge Class to create a database schema?
I'm using CodeIgniter 3.1.0 to develop an app. 我正在使用CodeIgniter 3.1.0开发应用程序。 In order to improve installation, I've written an Install_Controller, and an Install_Model.
为了改善安装,我编写了一个Install_Controller和一个Install_Model。
I want to use Database Forge class to manage the database schema, but it's not very clear in user guide and nothing on Google helps. 我想使用Database Forge类来管理数据库架构,但是在用户指南中不是很清楚,Google上没有任何帮助。
I need to use $this->dbforge->create_database , because the user knows nothing about database, so all he will do is MySQL "Next, next, install" and then run a batch file that run PHP as web server, so from Chrome he can use URL to install the app. 我需要使用$ this-> dbforge-> create_database ,因为用户对数据库一无所知,所以他所要做的就是MySQL“ Next,next,install”,然后运行一个将PHP作为Web服务器运行的批处理文件,因此Chrome他可以使用URL来安装应用程序。
User guide says: In order to initialize the Forge class, your database driver must already be running, since the forge class relies on it. 用户指南说: 为了初始化Forge类,您的数据库驱动程序必须已经在运行,因为forge类依赖于它。
So I have setup the config/database.php with user, pwd, dbname and so on... Even because I need it to use in app. 所以我已经用用户,pwd,dbname等设置了config / database.php 。即使我需要在应用程序中使用它也是如此。
When I try to load the URL to install the app, give me the error: Message: mysqli::real_connect(): (HY000/1049): Unknown database 'test' 当我尝试加载URL来安装应用程序时,给我错误: 消息:mysqli :: real_connect():(HY000 / 1049):未知数据库“测试”
So, how can I use Forge Class to create database schema, if I need to have it first? 因此,如果需要先拥有Forge类,该如何使用它创建数据库模式?
Some code... 一些代码...
$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!';
}
}
After try and get a lot of opinions, the only way is as I commented: 经过尝试并获得很多意见之后,唯一的方法就是我所说的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.