No adapter for Zend_Db_Table_Row type error?

I have a project where I am using multiple adapters. 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 in my controller i am trying:

$table = new MyModel();

      

when i fetch alll it works:

$results = $table->fetchAll(); // works fine

      

but when i try to filter it it doesn't work:

results = $ table-> fetchRow ("id = 1"); // Doesn't work. I get the error Error: No adapter for type MyRow.

Any idea? Thank you.

I forgot I am using paginator too

$paginator = Zend_Paginator::factory($results);

      

+2


source to share


1 answer


This is not where you should install the Db adapter for this table.

The method init()

is called after the table class has parsed its parameters and configured the adapter for the table. So, all you did was set the default Db adapter to build the table later, but it doesn't affect the current table if you do it in a method init()

.

Consider this simplified example:

class MyTable
{
  static $defaultDb;
  protected $db;

  static function setDefaultDb($db) { self::$defaultDb = $db; }

  public function __construct() {
    $this->db = self::$defaultDb;
    $this->init();
  }

  public function init() {
    // Unfortunately, PHP allows you to run static methods 
    // as if they are non-static methods, which is confusing.  
    $this->setDefaultDb($globalDb);
  }
}

      



This example is a simplified design model Zend_Db_Table

. Note that the method init()

sets the default Db of the class, but it runs after the constructor has already set the Db instance as the standard Db of the class. Therefore setting the Db class by default has no effect.

There are several ways to install the Db adapter for a table:

  • For all tables using the static method setDefaultAdapter()

    . The intended use case setDefaultAdapter()

    is as follows:

    Zend_Db_Table_Abstract::setDefaultAdapter($db);
    // now all tables will use $db by default
    $table = new MyModel();
    
          

  • As a constructor argument:

    $table = new MyModel(array('db'=>$db));
    
          

  • You can also use the method setOptions()

    after creating the table class.

    $table = new MyModel(); // uses default Db
    $table->setOptions(array('db'=>$otherDb));
    
          

    But keep in mind that the table reads its metadata from Db by default at build time, so if you change the adapter afterwards, the table must be defined the same in both databases.

+1


source







All Articles