DB_DataObject and multiple connections (DSNs)
| From: | Derek Downey | Date: | Wed, 03 Mar 2010 15:49:18 +0000 |
| Subject: | DB_DataObject and multiple connections (DSNs) | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-30350@lists.php.net to get a copy of this message | ||
Hi,
I would like to set up an application that utilizes a MySQL Master/Slave replication scheme. My application would write to the master and read from the slave. The application is currently using PEAR::DB_DataObject as its data abstraction layer. The app also has an abstract connector class that handles extra application-specific processing (such as tracking).
I noticed that DB_DO has the ability to use multiple databases, so I started playing with it. Here's my database configuration:
[DB_DataObject]
database_read = mysqli://USER:PASS@readDB.example.com/appDB
database_write = mysqli://USER:PASS@writeDB.example.com/appDB
....
...
The only way I found to switch the databases was the 'database' function. Unfortunately, this doesn't seem to switch out the DSN that matches the database and establish a new connection (or find a cached connection).
I am able to accomplish what I want by overwriting the database() function to clear out the _database_dsn_md5 value, forcing DB_DO to generate a new md5 based on the selected database. Then I override the find/query methods to use $this->database("read"); and the insert/update/delete methods to use $this->database("write");.
So my questions:
Has anyone else run into a similar scenario?
How did you accomplish it?
Is there a reason not to have the DB_DataObject::database() automatically clear out the md5 to force it to get the correct DSN?
Here's a brief example of what I did using the above ini configuration.:
BASIC APP DataObject
abstract class SomeBase extends DB_DataObject {
...
// OVERRIDE DB_DO FUNCTIONALITY //
public function database() {
$firstArg = func_get_arg(0);
// NO POINT GOING FURTHER IF DATABASE IS ALREADY SET TO WHAT WE HAVE //
if ($this->_database!=$firstArg) {
// reset md5 to force _connect to generate new based on database$this->_database_dsn_md5 = ''; parent::database($firstArg); // PASS ON FIRST ARGUMENT // } } public function insert() { $this->database("write"); ...// DO OTHER STUFF // parent::insert(); } public function update() { $this->database("write"); ...// DO OTHER STUFF // parent::update(); } public function delete() { $this->database("write"); ...// DO OTHER STUFF // parent::delete(); } public function find($fetchFirst=false) { $this->database('read'); return parent::find($fetchFirst); } public function query($stmt) { $this->database('read'); return parent::query($stmt); } } class Person extends SomeBase { ... } and here is a script that uses the classes: <?php $person = new Person(); $person->name = 'FOO'; $person->insert(); $person2 = new Person(); $person2->name = 'FOO'; $person2->find(true); ?> Thanks in advance for any comments, Derek Downey, DBA/Web Developer