2009-12-14 17 views
2

Dans un projet ZF en cours, je dois utiliser diffrent DB Connections pour lire et écrire. Mon approuch était le faire en étendant Zend_Db_Table_Abstract (et Zend_Db_Table_Row_Abstract)Zend_Db_Table Utilisation d'un adaptateur de connexion différent pour la lecture et l'écriture

Il ressemble à ceci au moment:

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; 
} 

}

à mon goût c'est bien trop cher code redondant. (Dans Zend_Db_Table_Row, je devrai aussi écraser les méthodes save et setFromArray)

Des suggestions? La commutation entre les deux DB Connections doit être aussi transparente que possible.

TIA Rufin

+0

J'ai posé une question similaire ici: http://stackoverflow.com/questions/1826798/master-slave-switch-in-the-zend-framework-application-layer – Pro777

Répondre

2

Votre code semble assez bon. Votre sous-classe doit de toute façon intercepter chaque méthode applicable, et je ne vois pas comment les bugs vont se glisser. La grande question: est-ce que le code fait l'affaire?

+0

J'ai mis à jour mon message sur le code maintenant en cours d'exécution. si longtemps aucun problème avec ça. – Rufinus

1

Il y a une autre façon de faire, montré here