2

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);
2
  • Can you please post the content of the MyRow Class? Do you realy need an own implementation of the Row Class? Commented Oct 5, 2009 at 11:38
  • I just declared the class. I guess there is no need for implementation. I just use it as row of the table. Commented Oct 5, 2009 at 14:08

1 Answer 1

1

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 the init() method.

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 model of the way Zend_Db_Table constructs. Note that the init() 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 use 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 might also be able to use the setOptions() method after the table class has been instantiated.

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

    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.

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

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.