Zend MultiDB适配器没有被切换

时间:2012-05-24 05:27:31

标签: zend-framework zend-db zend-db-table

我遇到了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);

    }
}

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

在控制器中调用模型时,您似乎需要传递数据库适配器实例:

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

希望这有帮助。

答案 1 :(得分:1)

我认为您需要拨打$this->_setAdapter($reader);而不是setDefaultAdapter()功能。

_setAdapter将新适配器设置为现有db表,而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);
}