问题描述
我正在编写一个应用程序,该应用程序需要在应用程序层内部进行主/从切换.现在,我在创建映射器时实例化Zend_Db_Table对象,然后将DefaultAdapter设置为从属.
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()
之前更改关联的表.
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应用程序层中的主/从开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!