1

我在使用 zend multidb 时遇到了一些问题。我的适配器没有被切换,我设置为默认值的适配器每次都被使用。而且它也没有给我任何错误。以下是我用于 zend multidb 功能的代码。

引导程序.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'));


}

应用程序.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);

    }
}

谁能帮我解决这个问题?

4

2 回答 2

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

希望这可以帮助。

于 2012-05-24T09:40:47.853 回答
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);
}
于 2012-05-24T20:30:55.243 回答