問題描述
我正在編寫一個應用程序,它需要在應用程序層內進行主/從切換.就像現在一樣,我在創建映射器時實例化一個 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切換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!