making $db variable available

For programming and general questions on Zend Framework

making $db variable available

Postby eleach on Thu May 07, 2009 12:29 am

I initialized a new Zend Framework project from within Zend Studio.

In bootstrap.php I set a db adapter:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => 'localhost',
'username' => 'user',
'password' => 'pass',
'dbname' => 'mydb'
));

In my controller file, I'm creating a new object that extends Zend_Db_Table:

include '/var/www/html/ztest/application/default/models/Browse.php'; (extends Zend_Db_Table)
$mytab = new Browse(array('db' => $db));

I'm getting the error $db is undefined. What is the right way to define the adapter so that it is available to the controller files?

Thanks
eleach
 
Posts: 2
Joined: Thu May 07, 2009 12:23 am

Re: making $db variable available

Postby ericritchie on Thu May 07, 2009 11:40 am

Hi Eleach,

The cleanest way would be to use the singleton design pattern to connect to your database. However this would also work (extract copied from the Framework documentation):

Code: Select all
Example 11.83. Example of constructing a Table using a Registry key
$db = Zend_Db::factory('PDO_MYSQL', $options);
Zend_Registry::set('my_db', $db);
// Later...
$table = new Bugs(array('db' => 'my_db'));


Reagrds,
Eric Ritchie.
User avatar
ericritchie
 
Posts: 119
Joined: Tue Feb 10, 2009 10:09 am

Re: making $db variable available

Postby rampog on Fri May 08, 2009 8:41 am

@ericritchie


what is the differences if i using zend_registry & using global keyword
Code: Select all
global $db




thanks before
rampog
 
Posts: 56
Joined: Mon May 04, 2009 12:15 pm


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 5 guests