2012-05-24 50 views
1

我在使用zend multidb時遇到了一些問題。我的適配器沒有被切換,而且我設置爲默認的每次都在使用。而且它也不會給我任何錯誤。 以下是我用於zend multidb功能的代碼。Zend MultiDB適配器沒有被切換

bootstrap.php中

public function _initDB() 
{ 
    Zend_Registry::getInstance();  
    $this->bootstrap('multidb'); 
    $multidb = $this->getPluginResource('multidb'); 
    Zend_Registry::set('dbR', $multidb->getDb('dbR')); 
    Zend_Registry::set('dbW', $multidb->getDb('dbW')); 


} 

的application.ini

resources.multidb.dbR.adapter = "mysqli" 
resources.multidb.dbR.host = "xxx.xxx.x.xx" 
resources.multidb.dbR.username = "root" 
resources.multidb.dbR.password = "admin" 
resources.multidb.dbR.dbname = "test_app1" 
resources.multidb.dbR.profiler = "false" 
resources.multidb.dbR.isDefaultTableAdapter = "true" 

resources.multidb.dbW.adapter = "mysqli" 
resources.multidb.dbW.host = "xxx.xxx.x.xx" 
resources.multidb.dbW.username = "root" 
resources.multidb.dbW.password = "admin" 
resources.multidb.dbW.dbname = "test_app2" 

現在在我的模型類我用下面的代碼行我想執行任何寫操作

class Abc_Model_ModelName extends Zend_Db_Table_Abstract 
{ 
    protected $_dbR; 
    protected $_dbW; 
    protected $_name = 'table_name'; 


    public function init(){ 
     $this->_dbR = Zend_Registry::get("dbR"); 
     $this->_dbW = Zend_Registry::get("dbW"); 
    } 

    public function addedit($data = array()) 
    { 
     $this->setDefaultAdapter($this->_dbW); 

    } 
} 

任何人都可以幫我解決這個問題嗎?

+0

它不應該是'$此 - > _ dbw-> getAdapter();或 - > getDb()'你可能不想重置你的默認適配器,但你確實想要檢索和第二個適配器的實例。我允許這種情況下的文檔不好,所以我也不確定。 – RockyFord

+0

@RockyFord我已經在我的引導文件中完成了這項工作,並存儲到註冊表中,並在Model類 –

回答

1

我相信你需要撥打$this->_setAdapter($reader);而不是setDefaultAdapter()函數。

_setAdapter會將新適配器設置爲現有數據庫表,而setDefaultAdapter()將僅設置將從現在開始使用的默認適配器。

喜歡的東西:

/** 
* Returns an instance of a Zend_Db_Table_Select object. 
* 
* @param bool $withFromPart Whether or not to include the from part of the select based on the table 
* @return Zend_Db_Table_Select 
*/ 
public function slaveSelect($withFromPart = self::SELECT_WITHOUT_FROM_PART) 
{ 
    $reader = $this->_getMultiDb()->getRandomReadOnlyAdapter(); 
    $this->_setAdapter($reader); 
    return parent::select($withFromPart); 
} 
+0

中檢索它,謝謝@aporat :) –

1

看起來你要麼需要通過DB適配器實例,當你調用模型在你的控制器中:

public function someAction() { 
    $db = Zend_Registry::get("dbW"); 
    $model = new Abc_Model_ModelName(array('db'=>$db)); 
} 

,或者你可以重寫構造函數模型類:

public function __construct() { 
    $this->_db = Zend_Registry::get("dbW"); 
    parent::__construct(); 
} 

數據庫適配器在Zend_Db_Table_Abstract的構造準備:

/** 
    * 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()) 
    { 
     /** 
     * Allow a scalar argument to be the Adapter object or Registry key. 
     */ 
     if (!is_array($config)) { 
      $config = array(self::ADAPTER => $config); 
     } 

     if ($config) { 
      $this->setOptions($config); 
     } 

     $this->_setup(); 
     $this->init(); 
    } 

希望這有助於。

相關問題