Zend_Db::factory() can take a native Config Object.
The config object below, would be used like so:
$config = Zend_Config_Xml('my-config.xml', 'application');
$db = Zend_Db::factory($config->database);
Note, the profiler line should also work ;)
<?xml version="1.0" encoding="UTF-8"?>
<myapp>
<application>
<debug>1</debug>
<database>
<adapter>PDO_MYSQL</adapter>
<initQuery>set names 'utf8';</initQuery>
<params>
<host>localhost</host>
<username>eric</username>
<password>mypass</password>
<dbname>devel</dbname>
<profiler>1</profiler>
</params>
</database>
</application>
</myapp>
Regards,
Eric
On Feb 6, 2008, at 1:20 AM, Garri Santos wrote:
On Feb 6, 2008 11:33 AM, Garri Santos
<[EMAIL PROTECTED]> wrote:
On Feb 6, 2008 11:22 AM, Matthew Weier O'Phinney <[EMAIL PROTECTED]>
wrote:
-- Garri Santos <[EMAIL PROTECTED]> wrote
(on Wednesday, 06 February 2008, 10:43 AM +0800):
Good Day,
Im not sure anymore whether it's my xml file or is it
Zend_Config_Xml
has the
problem. Here's my xml file.
config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<default>
<baseurl>/ubraa/public</baseurl>
<database>
<type>pdo_mysql</type>
<host>localhost</host>
<username>root</username>
<password>password</password>
<dbname>ubraa</dbname>
</database>
</default>
</configuration>
After doing this:
$this->config = new Zend_Config_Xml($this->ubraaRoot .
DIRECTORY_SEPARATOR .
'configuration' . DIRECTORY_SEPARATOR .
'config.xml', 'default');
The Zend_Db::factory($config->type, $config); is throwing me an
error
Um... shouldn't that be
Zend_Db::factory($config->database->type, $config->database);
?
Based on the structure of your XML, you're not pulling from the
correct
location in the config...
Adapter parameters must be in an array or a Zend_Config object
I have var_dump($this->config->database) and these is the result:
object(Zend_Config_Xml)#2 (6) {
["_allowModifications:protected"]=>
bool(false)
["_index:protected"]=>
int(0)
["_count:protected"]=>
int(2)
["_data:protected"]=>
array(2) {
["baseurl"]=>
string(13) "/ubraa/public"
["database"]=>
object(Zend_Config)#4 (6) {
["_allowModifications:protected"]=>
bool(false)
["_index:protected"]=>
int(0)
["_count:protected"]=>
int(5)
["_data:protected"]=>
array(5) {
["type"]=>
string(9) "pdo_mysql"
["host"]=>
string(9) "localhost"
["username"]=>
string(4) "root"
["password"]=>
string(8) "password"
["dbname"]=>
string(5) "ubraa"
}
["_loadedSection:protected"]=>
NULL
["_extends:protected"]=>
array(0) {
}
}
}
["_loadedSection:protected"]=>
string(7) "default"
["_extends:protected"]=>
array(0) {
}
}
Notice that the Zend_Config produce by the Zend_Config_Xml is
missing
"params"
w/c should contain a correct Zend_Config Object like this:
$configuration = new Zend_Config(
array(
'database' => array(
'adapter' => 'Mysqli',
'params' => array(
'dbname' => 'test',
'username' => 'webuser',
'password' => 'secret',
)
)
)
);
object(Zend_Config)#32 (6) {
["_allowModifications:protected"]=>
bool(false)
["_index:protected"]=>
int(0)
["_count:protected"]=>
int(1)
["_data:protected"]=>
array(1) {
["database"]=>
object(Zend_Config)#40 (6) {
["_allowModifications:protected"]=>
bool(false)
["_index:protected"]=>
int(0)
["_count:protected"]=>
int(2)
["_data:protected"]=>
array(2) {
["adapter"]=>
string(6) "Mysqli"
["params"]=>
object(Zend_Config)#43 (6) {
["_allowModifications:protected"]=>
bool(false)
["_index:protected"]=>
int(0)
["_count:protected"]=>
int(3)
["_data:protected"]=>
array(3) {
["dbname"]=>
string(4) "test"
["username"]=>
string(7) "webuser"
["password"]=>
string(6) "secret"
}
["_loadedSection:protected"]=>
NULL
["_extends:protected"]=>
array(0) {
}
}
}
["_loadedSection:protected"]=>
NULL
["_extends:protected"]=>
array(0) {
}
}
}
["_loadedSection:protected"]=>
NULL
["_extends:protected"]=>
array(0) {
}
}
Thanks,
Garri
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/
Hi Matthew,
I have tried your suggestion but still no luck. Here's the part
that uses
$this->config.
public function getDb()
{
if (null === $this->_services['db']) {
$config = $this->config->database;
//$this->_services['db'] = Zend_Db::factory($config->type,
$config);
$this->_services['db'] =
Zend_Db::factory($config->database->type, $config->database);
}
return $this->_services['db'];
}
As you can see I have already assigned $this->config->database to
$config.
Though I have tried what you have suggest again it's throwing me
the same
error.
Good Day,
It's finally working now thanks to all you guys here and SpotSec at
#zftalk.
Can we remove "or a Zend_Config object" in the Zend/Db.php line no.
217 it's
confusing since the code block is just checking if the passed
parameter
$config is an array.
/*
* Verify that adapter parameters are in an array.
*/
if (!is_array($config)) {
/**
* @see Zend_Db_Exception
*/
require_once 'Zend/Db/Exception.php';
throw new Zend_Db_Exception('Adapter parameters must be
in an
array or a Zend_Config object');
}
and the fix is to call toArray() method. Instead of passing a
Zend_Config
object for the $config parameters in Zend_Db::factory($adapter,
$config);
Cheers,
Garri