开发者

Zend_Db using multiple databases?

Is there a way I can use Zend_Db to make updates and insert crossing multiple (two) databases? Example,

UPDATE database1.tableA AS a1, databse2.tableA as a2 SET a1.content = a2.content WHERE a1.id = a2.id

How could I do this with Zend_Db_Adapter_Pd开发者_如何学Co_Mysql?


I have multiple databases defined in my application.ini file

I have used something like this:

// REGISTRY
Zend_Registry::set('configuration', $configuration);
foreach($configuration->database as $type => $database){
    $db[$type] = Zend_Db::factory(
        $database
    );
}
Zend_Registry::set('db', $db);

Upon creating your model you specify which db you want in the models constructor

$newModel = Model_NewModel($db['db_key_name']);

You then have 3 options, you can either manually code your own object to extend Zend_Db_Table_Abstract and have a multi-db-update function

/* the code for this example is pseudo code, so it probably wont work but the concept is still good */
class Model_NewModel extends MoakCustomDbClass{
    function multi_db_update($db1, $db2, $update, $where)
    {
        $originalDb = $this->_db;
        $this->_db = $db1;
        $this->update($update, $where);
        $this->_db = $db2;
        $this->update($update, $where);
        $this->_db = $originalDb;

    }
}

, write your own custom query by calling

$newModel->query('UPDATE QUERY');

or running 2 models both pointing at different databases

$newModelA = Model_NewModel($db['db_key_name_A']);
$newModelB = Model_NewModel($db['db_key_name_B']);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜