1

So I have multiple sqlite database. /path/database1.db /path/database2.db

When i make a Zend_Db_Table for it, how do I specify which db to use?

class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
    protected $_schema = 'database1.db';
    protected $_name = 'user';
}

This don't seem to work.

My current solution, I use multidb and extend Zend_Db_Table_Abstract and have an _setup overide like

...... Bootstrap - then use the multidb key for _database
$multiDb = $this->getPluginResource('multidb');
$multiDb->init();
Zend_Registry::set('db', $multiDb);
...... My/Table.php
class My_Table extends Zend_Db_Table_Abstract
{
    public function _setup()
    {
        if($this->_database) {
            $this->_setAdapter(Zend_Registry::get('db')->getDb($this->_database));
        }
    }
}
....... DbTables/Table1.php
class Application_Model_DbTable_Table1 extends My_Table
{
    protected $_database = 'database1';
    protected $_name = 'table1';
}

But I'm just wondering maybe there is already a way. Thanks

1 Answer 1

1

Easiest thing I can think of is to override Zend_Db_Table_Abstract::_setupDatabaseAdapter(), eg

protected function _setupDatabaseAdapter()
{
    if (!$this->_db) {
        $multiDb = Zend_Registry::get('multidb');
        $this->_setAdapter($multiDb->getDb('database1'));
    }
}

You won't need to add anything to your Bootstrap as application resources are automatically registered in Zend_Registry.

I'd even go further and place this into a custom abstract class, eg

abstract class Application_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
{
    protected function _setupDatabaseAdapter()
    {
        // as above
    }
}

Then have your concrete DbTable classes extend this.

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

2 Comments

It doesn't look like Zend_Registry auto adds my resources. anything special I need to turn on? This is the same as what I already have. I guess maybe there isn't a direct way yet. THanks
@Lasiaf My mistake. Whilst Zend_Application_Bootstrap_BootstrapAbstract uses Zend_Registry internally, it's not the globally available instance.

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.