开发者

How to use schema for multiple sqlite db for Zend_Db_Table

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/Tab开发者_如何学Gole.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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜