代码之家  ›  专栏  ›  技术社区  ›  Rufinus

Zend_DB_表使用不同的连接适配器进行读写

  •  2
  • Rufinus  · 技术社区  · 15 年前

    在当前的ZF项目中,我必须使用不同的DB连接进行读写。我的认可是通过扩展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_行中,我还必须覆盖save和setfromarray方法)

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

    蒂亚 鲁菲纳斯

    3 回复  |  直到 12 年前
        1
  •  2
  •   Derek Illchuk    15 年前

    你的代码看起来不错。不管怎样,您的子类必须截取每个适用的方法,我看不到bug是如何潜入的。大问题是:代码会起作用吗?

        2
  •  1
  •   Community c0D3l0g1c    7 年前

    有另一种方法可以做到这一点,如图所示 here

        3
  •  0
  •   eistrati    13 年前

    我找到了这篇文章 http://www.amazium.com/blog/using-different-databases-with-zend-framework 这有助于在zend_db_表中使用不同的连接适配器。