基本上,我所拥有的是:
class Database extends CI_Model
{
function __construct()
{
parent::__construct();
}
function connect($connection_infos)
{
$db = @new mysqli($connection_infos['host'], $connection_infos['username'],
$connection_infos['password'], $connection_infos['database']);
if (mysqli_connect_errno())
return FALSE;
else
return TRUE;
}
}
该模型加载到控制器的功能中:
class Management extends CI_Controller
{
static $dbs = array(
'ref' => array(
'connected' => FALSE,
),
'dest' => array(
'connected' => FALSE,
)
);
function connection()
{
$this->load->library('form_validation');
$data = array();
$this->form_validation->set_rules('host', 'Hostname', 'required');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('database', 'Database', 'required');
if (!$this->form_validation->run()) {
$data['dbs'] = self::$dbs;
} else {
$this->load->model('database'); // Here I load the model
$connection_infos = array(
'host' => $this->input->post('host'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'database' => $this->input->post('database'),
);
if ($this->database->connect($connection_infos)) {
self::$dbs['ref']['connected'] = TRUE;
$data['dbs'] = self::$dbs;
}
}
$this->load->view('dashboard', $data);
}
}
所以这就是我所做的:
在我看来,在表单验证中,我connection
从 Controller 调用该函数。该函数加载Database
模型,并调用模型的函数connect
。
我的问题是:如果我想在我的模型中创建其他功能来提出其他人的请求,我会每次都被迫打开一个连接吗?如果是,我如何“存储”连接凭据?
谢谢 !