I have a project in which I use more than one adapter.
So In ma models i created an abstract model
abstract My_Config1_Model extends Zend_Db_Table_Abstract
{
public function init()
{
$db = Zend_Registry::get('dbcon')->getDb(Kiga_Data_Database::MASTER);
$this->setDefaultAdapter($db);
}
}
and then I inherit this abstaract class like:
class MyModel extends My_Config1_Model
{
protected $_name = 'mytable';
protected $_primary = 'id';
protected $_rowClass = 'MyRow';
}
class MyRow extends Zend_Db_Table_Row_Abstract
{
}
and the in my controller I try:
$table = new MyModel();
when I fetch alll it works:
$results = $table->fetchAll(); // works fine
but when I try to filter it it does not work:
results = $table->fetchRow(“id = 1”); // Does not work. I get the error Error: No adapter for type MyRow.
Anybody any Idea?
Thanks.
I forgot I use also paginator
$paginator = Zend_Paginator::factory($results);
That’s not the place you should set the Db adapter for this table.
The
init()method is called after the table class has parsed its options and set up the adapter for the table. So all you’ve accomplished is to set the default Db adapter for subsequent table construction, but it has no effect on the current table if you do this in theinit()method.Consider this simplified example:
This example is a simplified model of the way
Zend_Db_Tableconstructs. Note that theinit()method sets the class default Db, but this is run after the constructor has already set the instance Db to be the class default Db. So setting the class default Db has no effect.There are several ways you can set the Db adapter for a table:
For all tables, using the static method
setDefaultAdapter(). The intended way to usesetDefaultAdapter()is as follows:As a constructor argument:
You might also be able to use the
setOptions()method after the table class has been instantiated.But be aware that the table reads its metadata from the default Db during construction, so if you change the adapter subsequently, the table should be defined identically in both databases.