Mark Dalby

Fighting Ignorance since 1986 (It’s taking longer than I thought).

Advertisement

Archive for the ‘ PHP ’ Category

Hi Guys,

This is the first in what will hopefully be a long series of “QuickCode” posts,

The concept is as follows…

The title of the post says what I’m doing, The content is the code to do it.  Questions will be answered in the comments.

So here goes…

——–

File: application/configs/application.ini

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = “Europe/London”
includePaths.library = APPLICATION_PATH “/../library”
bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
bootstrap.class = “Bootstrap”
appnamespace = “Application”
resources.frontController.controllerDirectory = APPLICATION_PATH “/controllers”
resources.frontController.params.displayExceptions = 0
resources.frontController.moduleDirectory = APPLICATION_PATH “/modules”
resources.layout.layoutPath = APPLICATION_PATH “/layouts/scripts/”
resources.view.helperPath = APPLICATION_PATH “/views/helpers”
resources.view.doctype = “XHTML1_TRANSITIONAL”
resources.modules = “”

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

resources.multidb.db1.adapter = “mysqli”
resources.multidb.db1.host = “DATABASE1 HOSTNAME”
resources.multidb.db1.username = “DATABASE1 USERNAME”
resources.multidb.db1.password = “DATABASE1 PASSWORD”
resources.multidb.db1.dbname = “DATABASE1 DBNAME”

resources.multidb.db2.adapter = “mysqli”
resources.multidb.db2.host = “DATABASE2 HOSTNAME”
resources.multidb.db2.username = “DATABASE2 USERNAME”
resources.multidb.db2.password = “DATABASE2 PASSWORD”
resources.multidb.db2.dbname = “DATABASE2 DBNAME”

File: application/Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
*
* Database initialisation :: this method parses the main ini file and creates registry keys from all multidb vars.
*/

public function _initDatabase(){
$resource = $this->getPluginResource(‘multidb’);
$resource->init();
Zend_Registry::set(‘db1′,$resource->getDb(‘db1′));
Zend_Registry::set(‘db2′,$resource->getDb(‘db2′));

}
}

To use with Zend_Db_Table_Abstract

$results = new Application_Model_DbTable_MyDB(array(‘db’ => ‘db1));

To use away from the Zend_Dbtable_Abstract classes.

class Application_Model_MyModel{

// Define variables to use as class wide containers.
protected $db1; // First database container.
protected $db2; // second database container.

/**
* @purpose to load database connections into the model.
*/

public function __construct(){
$this->db1 = Zend_Registry::get(‘db1′); // init db1 database.
$this->db2 = Zend_Registry::get(‘db2′); // init db2 database.
}

public function SampleMethod(){
$sql = “SELECT * FROM mydatabase “;
$resultset = $this->db1->query($sql);
$moresql = “SELECT * FROM mydatabase2 “;
$resultset = $this->db2->query($moresql);
}

Well I hope this helps you guys, it took me a while to figure it out.

Enhanced by Zemanta