Master / Slave switch in the Zend Framework application layer(Zend Framework应用层的Master/Slave切换)
问题描述
我正在编写一个应用程序,它需要在应用程序层内进行主/从切换.就像现在一样,我在创建映射器时实例化一个 Zend_Db_Table 对象,然后 setDefaultAdapter 到从属设备.
I am writing an application which requires the Master/Slave switch to happen inside the application layer. As it is right now, I instantiate a Zend_Db_Table object on creation of the mapper, and then setDefaultAdapter to the slave.
现在在基本映射器类中,我有以下方法:
Now inside of the base mapper classe, I have the following method:
public function useWriteAdapter()
{
if(Zend_Db_Table_Abstract::getDefaultAdapter() != $this->_writeDb)
{
Zend_Db_Table_Abstract::setDefaultAdapter($this->_writeDb);
$this->_tableGateway = new Zend_Db_Table($this->_tableName);
}
}
我需要对此进行完整性检查.我不认为开销太大,我只是怀疑一定有更好的方法.
I need a sanity check on this. I don't think the overhead is too much, I just suspect there must be a better way.
推荐答案
Zend_Db_Table_Row_Abstract
类型的对象会记住产生它的 Table 对象.但是您可以在调用 save()
之前更改关联的 Table.
An object of type Zend_Db_Table_Row_Abstract
remembers what Table object produced it. But you can change the associated Table before you call save()
.
$readDb = Zend_Db::factory(...); // replica
$writeDb = Zend_Db::factory(...); // master
Zend_Db_Table::setDefaultAdapter($readDb);
$myReadTable = new MyTable(); // use default adapter
$myWriteTable = new MyTable($writeDb);
$row = $myTable->find(1234)->current();
$row->column1 = 'value';
$row->setTable($myWriteTable);
$row->save();
这篇关于Zend Framework应用层的Master/Slave切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Zend Framework应用层的Master/Slave切换


- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01