|
View:
New views
9 Messages
—
Rating Filter:
Alert me
|
|
|
Dumb QuestionIn my index.php bootstrap file I setup the database connection into a
variable $db. Now I need to access this in other controllers. This seems like a dumb question to me but do I need to global $db; before I can access it in the indexAction method of the controller or is it in the global scope by default? Thanks, Joseph Crawford |
|
|
|
|
|
|
|
|
RE: Dumb QuestionAFAIK It is not global by default, you could probably find a few neater ways to do this but I just use |
|
|
Re: Dumb QuestionYou can always just set it in Zend_Registry too. In my bootstrap, I save
it to the registry after I make my $db object so that I have access to it on the off-chance my Controllers need to run a direct DB query. Chris Joseph Crawford wrote: > In my index.php bootstrap file I setup the database connection into a > variable $db. Now I need to access this in other controllers. This > seems like a dumb question to me but do I need to global $db; before I > can access it in the indexAction method of the controller or is it in > the global scope by default? > > Thanks, > Joseph Crawford > -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. |
|
|
Re: Dumb Questionif you are using Zend_Controller_Front as I am you can set it as a parameter:
$frontController = Zend_Controller_Front::getInstance(); $frontController->setParam('db', $db) and then in your controller: $this->getFrontController()->getParam('db') So many ways to skin a cat. Jim
|
|
|
RE: Dumb QuestionI usually access the database through my table objects.
However, recently I've needed to do things directly with the database object. I found the simplest choice was $GLOBALS['db'] = $db. It's so rare that I directly use it, and a global lookup is pretty cheap (right?). Anytime I need an object instantiated in my bootstrap I'll assign it to a global. If I access it more than once in a function, I'll either localize the variable or try and get it a more reliable way. I didn't think about using: Zend_Db_Table_Abstract::getDefaultAdapter(); That seems like it might be a good alternative choice. Kevin Hallmark PHP Developer Bonnier Corporation -----Original Message----- From: Jim Scherer [mailto:jscherer26@...] Sent: Thursday, May 15, 2008 10:12 AM To: fw-general@... Subject: Re: [fw-general] Dumb Question if you are using Zend_Controller_Front as I am you can set it as a parameter: $frontController = Zend_Controller_Front::getInstance(); $frontController->setParam('db', $db) and then in your controller: $this->getFrontController()->getParam('db') So many ways to skin a cat. Jim Joseph Crawford wrote: > > In my index.php bootstrap file I setup the database connection into a > variable $db. Now I need to access this in other controllers. This > seems like a dumb question to me but do I need to global $db; before I > can access it in the indexAction method of the controller or is it in > the global scope by default? > > Thanks, > Joseph Crawford > > -- View this message in context: http://www.nabble.com/Dumb-Question-tp17253025p17254165.html Sent from the Zend Framework mailing list archive at Nabble.com. |
|
|
Re: Dumb QuestionI create a $db in my bootstrap and save a reference in Zend_Registry. I use getDefaultAdapter() whenever possible but I have some models with static methods, which by definition can't reference $this, so they pull it from the registry.
On Thu, May 15, 2008 at 11:10 AM, Kevin Hallmark <Kevin.Hallmark@...> wrote: <snip/>I usually access the database through my table objects. -- David Mintz http://davidmintz.org/ The subtle source is clear and bright The tributary streams flow through the darkness |
|
|
Re: Dumb QuestionHi Joseph,
What I usually do is three fold: 1. Add is as the default adapter to Zend_Db_Table for use by all my Zend based Models. This also acts as a Registry by the way, since you can call Zend_Db_Table::getDefaultAdapter() anywhere. 2. Add it to a Registry as would be normal practice. 3. Possibly add it as a Front Controller parameter so you can retrieve it from any controller action using that method. Where possible I prefer 3, simply because I prefer to avoid as many static calls in my controllers as possible. But a Registry works just fine so long as you use it only as needed. Paddy
Pádraic Brady
http://blog.astrumfutura.com http://www.patternsforphp.com OpenID Europe Foundation - Irish Representative |
| Free Forum Powered by Nabble | Forum Help |