1

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'));


}

アプリケーション.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

$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);
}
于 2012-05-24T20:30:55.243 に答える
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 に答える