gpt4 book ai didi

php - 具有 pdo 的数据库类,在继承时不应重新启动连接

转载 作者:可可西里 更新时间:2023-11-01 08:44:44 24 4
gpt4 key购买 nike

当我一次调用不同的模型然后多次建立数据库连接时,我只想要一个,这可能吗?下面是我的 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(),这里会看到每次建立数据库连接时,有时我会遇到连接过多错误,或者在某些系统中我会遇到内存分配问题。

请帮我解决这个问题。

最佳答案

尝试在 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;
}
}

关于php - 具有 pdo 的数据库类,在继承时不应重新启动连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31972978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com