So I designed a system that extends the PDO class as such...
class PdoConfig extends PDO {
private $engine;
public $host;
public $database;
public $user;
public $pass;
public function __construct(){
// DB CONFIG
if (DEVELOPMENT) {
$dbhost = 'localhost';$dbuser = 'blah';$dbpass = 'blah';$dbname = 'blah';
} elseif (STAGING) {
$dbhost = '199.79.xxx.xxx';$dbuser = 'blah';$dbpass = 'blah';$dbname = 'blah';
} else {
$dbhost = '199.79.xxx.xxx';$dbuser = 'blah';$dbpass = 'blah';$dbname = 'blah';
}
$this->engine = 'mysql';
$this->host = $dbhost;
$this->database = $dbname;
$this->user = $dbuser;
$this->pass = $dbpass;
$dsn = $this->engine.':dbname='.$this->database.";host=".$this->host;
self::__construct( $dsn, $this->user, $this->pass );
}
public function reconnect() {
try {
$this->query('SELECT 1');
} catch (PDOException $e) {
$dsn = $this->engine.':dbname='.$this->database.";host=".$this->host;
self::__construct( $dsn, $this->user, $this->pass );
}
return true;
}
}
and then I use this PDOConfig class as the base class for all my model classes that require DB access, as such...
class dbHealthNewsContent extends PdoConfig { ... }
class dbHealthVideoContent extends PdoConfig { ... } etc
this has allowed me to make db queries where the PDO connections and statements are part of the class ie
public function get_fbmessage () {
$article_id = $this->row['id'];
$sql = "SELECT fb_message FROM fb_post
WHERE article_id = $article_id LIMIT 1";
$stm = $this->prepare($sql);
$stm->execute();
if ($stm->rowCount() == 1) { ... }
}
and it's worked quite well up to now.
So the issue is that due to the popularity of the app, we are determining that too many connections are being made and performance is suffering and I have been trying to figure out how to make the PDOConfig class into either a global, sharable object or a singleton class, but without returning an "instance" and rather using a shared PDO connection within the class, transparent to all the derived classes.
Basically, I am trying to do a refactor by fixing the issues within the class itself and not having to go thru the entire codebase, changing all the superclass models.
Any insights or comments welcome.
Aucun commentaire:
Enregistrer un commentaire