2

I am trying to register my default database adapter in my bootstrap.php file so that I can access it where ever I am. This is my code so far:

//bootstrap.php
protected function _initDb()
{
    $dbAdapter = Zend_Db::factory(Zend_Registry::get('configuration')
                                       ->resources->db->adapter, 
                                  Zend_Registry::get('configuration')
                                       ->resources->db->params->toArray());

    Zend_Registry::set('dbAdapter', $dbAdapter);
    Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
}

I am then trying to call it in one of my models by saying:

//exampleModel.php
$select = $this->_getDbAdapter()
                ->select()
                ->from(array('t' => $this->_getTable()->getName()), 
                       array('name'))....

However I am just getting the error:

Call to undefined method Application_Model_Example::_getdbAdapter() in...

So obviously it is looking for it within my current class and can't find it...

4
  • Have you tried doing this in application.ini? I think that's a better place to do it. Commented Aug 10, 2011 at 13:50
  • doing what???? i have declared my db host, name and password etc in application ini but i am noe trying to create the adapter Commented Aug 10, 2011 at 13:51
  • it's already created. have you read this? framework.zend.com/manual/en/… Commented Aug 10, 2011 at 13:54
  • please show your Application_Model_Example class Commented Aug 10, 2011 at 14:09

3 Answers 3

1

You need this in your Model_Example

public function _getSqlAdapter()
{
    return Zend_Registry::get('dbAdapter');
}

Or directly call Zend_Db_Table::getDefaultAdapter() instead of $this->_getDbAdapter()

Sign up to request clarification or add additional context in comments.

2 Comments

thanks man so obvious now i see... is there somewhere i can put this method so that i dont have to have it in every model and i can just say $select = $this->_getDbAdapter()?
You can create an Abstract Model and extend it inside all your models or just call Zend_Db_Table::getDefaultAdapter()
0

In the code provided you don't appear to be calling it the adapter from the registry. You would need to use Zend_Registry::get('dbAdapter');

What class does Application_Model_Example extend?

Comments

0

I have Zend_Db_Table::setDefaultAdapter($dbAdapter); in my bootstrap (notice its Zend_Db_Table, not Zend_Db_Table_Abstract).

Then in my models, I would just use

$select = $this->->select()
    ->from(array('t' => $this->_getTable()->getName()), array('name'))....

assuming your model extends Zend_Db_Table?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.