Zend_Db_Table 使用不同的 Connection-Adapter 进行读写

发布于 2024-08-14 21:39:57 字数 3194 浏览 8 评论 0原文

在当前的 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 方法)

对此有什么建议吗?两个数据库连接之间的切换应尽可能透明。

TIA 鲁菲努斯

In a current ZF project i have to use diffrent DB Connections for reading and writing. My approuch was do this by extending Zend_Db_Table_Abstract (and Zend_Db_Table_Row_Abstract)

It looks like this at the 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;
} 

}

For my liking this is way to much redundant code. (In Zend_Db_Table_Row i will also have to overwrite the save and setFromArray methods)

Any suggestions on this ? The switching between the two DB Connections should be as transparent as possible.

TIA
Rufinus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-08-21 21:39:57

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

Your code looks pretty good. Your subclass has to intercept each applicable method anyway, and I don't see how bugs will creep in. The big question: does the code do the trick?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文