当我在一个中调用不同的模型然后多次建立db连接时,我只想要一个,是否可能。下面是我的mvc
class Database{
private $db;
private $stmt;
function __construct()
{
parent::__construct();
$root = dirname(dirname(__FILE__));
$this->config = parse_ini_file($root . '/app/config.ini', true);
$db = $this->config['database settings'];
$host = $db['host'];
$user = $db['user'];
$pword = $db['pword'];
$db_name = $db['db'];
$this->db = new PDO("mysql:host=$host;dbname=$db_name", $user, $pword, array(
PDO::MYSQL_ATTR_FOUND_ROWS => true, PDO::ATTR_PERSISTENT => true
));
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
function executeQuery($query, $params = array())
{
$this->stmt = $this->db->prepare($query);
if (! ($this->stmt)) {
throw new Exception('Query failed while preparing');
}
try {
$this->stmt->execute($params);
} catch (PDOException $e) {
throw $e;
}
}
function getRecords($query, $params = array(), $array = false, $all = false)
{
$this->executeQuery($query, $params);
$records = array();
if ($this->totalRecords() > 0) {
if ($array) {
$this->stmt->setFetchMode(PDO::FETCH_ASSOC);
} else {
$this->stmt->setFetchMode(PDO::FETCH_OBJ);
}
if ($all) {
$records = $this->stmt->fetchAll();
} else {
while (($record = $this->stmt->fetch()) !== false) {
$records[] = $record;
}
}
}
return $records;
}
}
以下是我使用它的模型
class TestModel extends Database{
function __construct(){
parent::__construct();
}
function getUsers(){
return $this->getRecords("SELECT * FROM users");
}
}
下面是另一个模型,我想调用方法getusers将其用于某种目的。
require("path_to_testmodel");
require("path_to_some_model");
require("path_to_some_other_model");
class TestModel2 extends Database{
function __construct(){
parent::__construct();
$this->test_model = new TestModel();
$this->some_model = new SomeModel();
$this->some_other_model = new SomeOtherModel();
}
function doSomething(){
$users = $this->test_model->getUsers();
}
}
如果我在其中一个模型中包含多个模型,如TestModel(),这里看到的是每次建立数据库连接时,有时我会得到太多的连接错误,或者在某些系统中我会遇到内存分配问题。
请帮我解决这个问题。
答案 0 :(得分:1)
尝试在static property。
中保存您的数据库连接class Database{
static private $theOnlyConnection = null;
private $db;
private $stmt;
public function __construct()
{
parent::__construct(); // Database has no parent, what is this line for?
if(null === self::$theOnlyConnection) {
$root = dirname(dirname(__FILE__));
$config = parse_ini_file($root . '/app/config.ini', true);
$db = $config['database settings'];
$host = $db['host'];
$user = $db['user'];
$pword = $db['pword'];
$db_name = $db['db'];
self::$theOnlyConnection = new PDO("mysql:host=$host;dbname=$db_name", $user, $pword, array(
PDO::MYSQL_ATTR_FOUND_ROWS => true, PDO::ATTR_PERSISTENT => true
));
self::$theOnlyConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$theOnlyConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
$this->db = self::$theOnlyConnection;
}
请注意,这将导致在脚本结束时关闭连接,而不是在对象销毁时关闭。对于大多数用例来说都没问题,但是如果你想在之前断开与数据库的连接,你需要跟踪你有多少个对象,并在数字为0时销毁连接。
class Database{
static private $theOnlyConnection = null;
static private $modelObjectCount = 0;
private $db;
private $stmt;
public function __construct()
{
self::$modelObjectCount++;
if(null === self::$theOnlyConnection) {
// connect
}
$this->db = self::$theOnlyConnection;
}
public function __destruct() {
self::$modelObjectCount--;
if(0 == self::$modelObjectCount) {
// close connection
self::$theOnlyConnection = null;
}
}