Zend_Db 使用多个数据库?

发布于 2024-11-17 03:48:14 字数 232 浏览 2 评论 0 原文

有没有办法使用 Zend_Db 进行更新并跨多个(两个)数据库插入? 例如,

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

我如何使用 Zend_Db_Adapter_Pdo_Mysql 做到这一点?

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_Pdo_Mysql?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情魔剑神 2024-11-24 03:48:14

我在 application.ini 文件中定义了多个数据库,

我使用了这样的内容:

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

在创建模型时,您在模型构造函数中指定您想要的数据库

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

然后您有 3 个选项,您可以手动编写自己的对象来扩展 Zend_Db_Table_Abstract 和具有多数据库更新功能

/* 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;

    }
}

来编写您自己的自定义查询

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

,通过调用或运行两个指向不同数据库的模型

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

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']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文