2009-12-14 31 views
2

在當前的ZF項目中,我必須使用不同的數據庫連接進行讀取和寫入。我approuch是通過擴展Zend_Db_Table_Abstract(和Zend_Db_Table_Row_Abstract)做到這一點Zend_Db_Table使用不同的連接適配器進行讀取和寫入

它看起來像這樣的時刻:

class SomeNamespace_Db_Table extends Zend_Db_Table_Abstract { 
/** 
* @var Zend_Db 
*/ 
protected $read = NULL; 

/** 
* @var Zend_Db 
*/ 
protected $write = NULL; 

/** 
* Constructor. 
* 
* Supported params for $config are: 
* - db    = user-supplied instance of database connector, 
*      or key name of registry instance. 
* - name   = table name. 
* - primary   = string or array of primary key(s). 
* - rowClass  = row class name. 
* - rowsetClass  = rowset class name. 
* - referenceMap = array structure to declare relationship 
*      to parent tables. 
* - dependentTables = array of child tables. 
* - metadataCache = cache for information from adapter describeTable(). 
* 
* @param mixed $config Array of user-specified config options, or just the Db Adapter. 
* @return void 
*/ 
public function __construct($config=array()){  
    $this->read = Zend_Registry::get('read'); 
    $this->write = Zend_Registry::get('write'); 

    $config['db'] = $this->read;    
    return parent::__construct($config); 
} 

/** 
* Inserts a new row. 
* 
* @param array $data Column-value pairs. 
* @return mixed   The primary key of the row inserted. 
*/  
public function insert(array $data){ 
    $this->setAdapter($this->write); 
    $result = parent::insert($data); 
    $this->setAdapter($this->read); 

    return $result; 
} 

/** 
* Updates existing rows. 
* 
* @param array  $data Column-value pairs. 
* @param array|string $where An SQL WHERE clause, or an array of SQL WHERE clauses. 
* @return int   The number of rows updated. 
*/  
public function update(array $data, $where){ 
    $this->setAdapter($this->write); 
    $result = parent::update($data,$where); 
    $this->setAdapter($this->read); 

    return $result; 
} 

/** 
* Fetches a new blank row (not from the database). 
* 
* @param array $data OPTIONAL data to populate in the new row. 
* @param string $defaultSource OPTIONAL flag to force default values into new row 
* @return Zend_Db_Table_Row_Abstract 
*/  
public function createRow(array $data = array(), $defaultSource = NULL){ 
    $this->setAdapter($this->write); 
    $result = parent::createRow($data, $defaultSource); 
    $this->setAdapter($this->read); 

    return $result; 
} 

/** 
* Deletes existing rows. 
* 
* @param array|string $where SQL WHERE clause(s). 
* @return int   The number of rows deleted. 
*/  
public function delete($where){ 
    $this->setAdapter($this->write); 
    $result = parent::delete($where); 
    $this->setAdapter($this->read);  

    return $result; 
} 

/** 
* Allow to set current used connection 
* from Enalog_Db_Table_Row 
* 
* @param Zend_Db $db 
*/  
public function setAdapter($db){ 
    $this->_db = self::_setupAdapter($db); 
    return $this; 
} 

}

我喜歡,這是辦法不多冗餘代碼。 (在Zend_Db_Table_Row我也將不得不覆蓋save和setFromArray方法)

對此的任何建議?兩個DB連接之間的切換應儘可能透明。

TIA Rufinus

+0

我在這裏問了一個類似的問題:http://stackoverflow.com/questions/1826798/master-slave-switch-in-the-zend-framework-application-layer – Pro777 2009-12-14 20:07:08

回答

2

您的代碼看起來不錯。無論如何,你的子類必須攔截每個可用的方法,而且我也沒有看到bug會如何蔓延。一個大問題是:代碼是否有用?

+0

我更新了我的帖子到現在運行的代碼。這麼長時間沒有問題。 – Rufinus 2010-01-14 09:51:38

1

還有另一種方式來做到這一點,顯示here

相關問題