php - Zend_Db_Table 使用不同的连接适配器进行读写

标签 php zend-framework zend-db-table

在当前的 ZF 项目中,我必须使用不同的数据库连接进行读写。我的方法是通过扩展 Zend_Db_Table_Abstract(和 Zend_Db_Table_Row_Abstract)来做到这一点

目前看起来是这样的:

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 Connections 之间的切换应该尽可能透明。

TIA
鲁菲努斯

最佳答案

你的代码看起来不错。无论如何,您的子类必须拦截每个适用的方法,而且我看不出错误会如何蔓延。最大的问题是:代码能解决问题吗?

关于php - Zend_Db_Table 使用不同的连接适配器进行读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1901579/

相关文章:

php - Zend 错误字符串

zend-framework - Zend DB 更新中的 OR 子句?

php - 使用 Zend_Db 类避免 MySQL 注入(inject)

php - 发送 CodeIgniter 奇怪的电子邮件

php - Jquery - 如何使用带有复选框的 Hide() 来启用动画(过渡)?

javascript - AJAX POST 不工作,php 不创建 session

mysql - PostgreSQL 上的 Doctrine SQL 查询 : works on MySQL, 不会

php - 使用 PHP 将图像 blob 从 mysql 数据行插入 mysql 数据库

php - Zend 文件字段的错误 - 文件名不会以 zend 形式重新填充

php - 如何在 Zend_Form 中使用来自 Zend_Db_Table 的数据