-- Dan Rossi <[EMAIL PROTECTED]> wrote
(on Thursday, 02 August 2007, 08:50 PM +1000):
> Another example for instance, I'll have to duplicate this across controllers
>
> $this->db = Zend_Registry::get('db');
>
> try {
> $this->db->getConnection();
> } catch (Zend_Db_Adapter_Exception $e) {
> var_dump($e);
> // perhaps a failed login credential, or perhaps the RDBMS
> is not running
> } catch (Zend_Exception $e) {
> var_dump($e);
>
> // perhaps factory() failed to load the specified Adapter class
> }
Use an action helper for this. For example:
class My_Helper_Db extends Zend_Controller_Action_Helper_Abstract
{
public $db;
public function direct()
{
if (null === $this->db) {
$this->db = Zend_Registry::get('db');
try {
$this->db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
// ...
} catch (Zend_Exception $e) {
// ...
}
}
$this->getActionController()->db = $this->db;
}
}
Add the helper to the helper broker, either by path or prefix:
// in your bootstrap:
Zend_Controller_Action_HelperBroker::addPrefix('My_Helper');
Then, in your action controller, when you need to make sure the DB is
present:
$this->_helper->db(); // init DB connection
$this->db->....; // do something with the db...
That's it.
> Jack Sleight wrote:
> >I solve this by creating an ApplicationController which extends
> >Zend_Controller_Action, and then all my other controllers extend that.
> >Just make sure to include the file, because ZF wont include it
> >automatically.
> >
> >Dan Rossi wrote:
> >>Hi there I meant a common class the controllers can extend to reuse
> >>some functionality, ie getting the registries set in the bootstrap ,
> >>etc. Then it means its only editable in one spot. Maybe via a plugin ?
> >>
> >>Karol Grecki wrote:
> >>
> >>
> >
>
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/