基本的に、私が持っているのは次のようなものです:
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
コントローラーから関数を呼び出します。この関数は、モデルをロードし、Database
モデルの関数を呼び出しますconnect
。
私の質問は次のとおりです。自分のモデルで他の関数を作成して他の要求を作成したい場合、毎回接続を開く必要がありますか? はいの場合、接続資格情報を「保存」するにはどうすればよいですか?
ありがとう !