Hi,
1st Step:
Store your DB parameters in a configuration file.
Typically, I store theses parameters in a config.xml file (in
/configs directory)
The XML content could be :
<database>
<adapter>Pdo_Pgsql</adapter>
<params>
<host>localhost</host>
<username>postgres</username>
<password> </password>
<dbname>db_name</dbname>
</params>
</database>
2nd Step:
In your bootstrap, load the configuration file
$frontofficeConfig = new Zend_Config_Xml('../../configs/config.xml', '');
Zend_Registry::set('frontofficeConfig', $frontofficeConfig);
=> I often put this configuration in the Zend Registry in order to access
everywhere in my application.
Final Step: The connection
In your bootstrap, create your connection, store it in the Zend Registry and
you'll be able to use it everywhere in your application
The code could be:
try {
$objDbSource = Zend_Db::factory($frontofficeConfig->database);
$objDbSource->getConnection();
$objDbSource->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('objDbSource', $objDbSource);
} catch (Zend_Db_Adapter_Exception $e) {
// perhaps a failed login credential, or perhaps the RDBMS is not
running
throw new Exception('Fatal Error : Unable to connect to Database
Server', $e);
} catch (Zend_Exception $e) {
// perhaps factory() failed to load the specified Adapter class
throw new Exception('Fatal Error : Unable to load the Adaptater',
$e);
}
Kind regards,
Guillaume BABIK
INTERNIM
74, rue Baudin
92300 LEVALLOIS
FRANCE
Tel/Fax : 01.40.87.11.20
Mobile : 06.80.21.90.29
http://www.internim.com
-----Message d'origine-----
De : Tom Printy [mailto:[EMAIL PROTECTED]
Envoyé : samedi 28 juin 2008 21:35
À : [email protected]
Objet : [fw-general] DB connection params
Hello,
I am just starting with the Zend framework. I am using the MVC
fucntionality and would like to know the best practice for configuring
and using DB connection paramaters. I don't think I want to this in
every function that connects to a db.
params = array(
'host' => 'localhost',
'username' => 'postgres',
'password' => '',
'dbname' => ''
);
$DB = Zend_Db::factory('pdo_pgsql', $params);
Thanks
-Tom Printy