View Source


<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_DB
{zone-data}

{zone-data:proposer-list}
[Daniel Schwiperich|mailto:ds@ms-computerservice.com]
{zone-data}

{zone-data:revision}
1.1 - 1 August 2006: Updated from community comments.
{zone-data}

{zone-data:overview}
(First) Get Zend_DB.::factory with a Zend_Config object working
{zone-data}

{zone-data:references}
*
{zone-data}

{zone-data:requirements}
*
{zone-data}

{zone-data:dependencies}
* Zend_DB
* Zend_Config
{zone-data}

{zone-data:operation}
Zend_DB::factory only accepts an array as param.
To get compatible to Zend_Config it has also to accept an object of type Zend_Config
I've changed therefore the factorymethod a little bit, that it'll accept a Zend_Config object
and turns the object into an array. If the object contains other types than Strings an exception will be thrown.
See the example below...
{zone-data}

{zone-data:class-list}
* Zend_DB
* Zend_Config
{zone-data}

{zone-data:use-cases}
||UC-01||
An example config.xml
{code}
<?xml version="1.0" encoding="UTF-8"?>
<config>
<development>
<webhost>localhost</webhost>
<database>
<dbtype>PDO_MYSQL</dbtype>
<host>localhost</host>
<username>root</username>
<password>xxx</password>
<dbname>xxxx</dbname>
</database>
</development>
</config>
{code}

The DB-Init in index.php
{code}
//...
$config = new Zend_Config(Zend_Config_Xml::load(CONFIG.DIRECTORY_SEPARATOR.'config.xml','development'));
$db = Zend_Db::factory($config->database->dbtype, $config->database);
//...
{code}
{zone-data}

{zone-data:skeletons}


The updated DB_Zend::factory method. Changes between the //----- comments
{code}
static public function factory($adapterName, $config = array())
{
if (!is_string($adapterName) or !strlen($adapterName)) {
throw new Zend_Db_Exception('Adapter name must be specified in a string');
}
//-------------------------------------------------------------------------------------------------
if (!is_array($config) && !is_a($config, 'Zend_Config')) {
throw new Zend_Db_Exception('Configuration must be an array or a Zend_Config-object');
}

//Converts Zend_Config object to an array
if(is_a($config, 'Zend_Config')){
$tmp = array();
foreach($config as $attribute => $val){
if(is_string($val)){
$tmp[$attribute] = $val;
}
else throw new Zend_Db_Exception('Your Zend_Config object must only contains values of the type String. Found a:'.gettype($val));
}
$config = $tmp;
$tmp = null;
}
//-------------------------------------------------------------------------------------------------
$adapterName = strtolower($adapterName); // normalize input
if (substr($adapterName, 0, 3) === 'pdo') {
if ($adapterName{3} !== '_') // this check will be removed, and underscores will be required in 0.2
trigger_error("Use of adapter name '$adapterName' is deprecated. Underscores will be required in 0.2 release. Please use 'PDO_".(strtoupper(substr($adapterName,3)))."'.", E_USER_WARNING);
$adapterName = 'Zend_Db_Adapter_Pdo_' . ucfirst(ltrim(substr($adapterName, 3),'_'));
} else {
$adapterName = 'Zend_Db_Adapter_' .
str_replace(' ',
'_' ,
ucwords(str_replace('_', ' ', $adapterName)));
}

Zend::loadClass($adapterName);

return new $adapterName($config);
}
{code}

{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>