开发者

Filter SELECT and INSERT/UPDATE/DELETE queries in Zend Framework

开发者 https://www.devze.com 2023-01-29 09:45 出处:网络
I am using multidb pattern in Zend Framework. Typically I will be using master/slave architecture of MysqlDB.

I am using multidb pattern in Zend Framework.

Typically I will be using master/slave architecture of MysqlDB.

So my question is what should I do to execute SELECT queries from slave database and INSERT/UPDATE/DELETE queries on master database

My application.ini looks like

re开发者_JS百科sources.multidb.primary.adapter = PDO_MYSQL
resources.multidb.primary.host = localhost
resources.multidb.primary.username = root
resources.multidb.primary.password = 123456
resources.multidb.primary.dbname = tubaah_zend
resources.multidb.primary.default = true

resources.multidb.secondary.adapter = PDO_MYSQL
resources.multidb.secondary.host = localhost
resources.multidb.secondary.username = root
resources.multidb.secondary.password = 123456
resources.multidb.secondary.dbname = tubaah

So I want to run all SELECT queries on secondary database and all INSERT/UPDATE/DELETE on primary database.


I believe insert/update/delete should work just fine, i.e.:

My_Model_DbTable_MyTable.php:

function myFunction() {
    $this->insert()
    $this->update()
    $this->delete()
}

However, if you wish to use the secondary database, you may be unable to use the typical $this->select() method:

My_Model_DbTable_MyTable.php

// Override getAdapter() function to be able to obtain secondary database
function getAdapter($name = 'primary') {
    $resource = $this->getPluginResource('multidb');
    $resource->init();

    // Ensure only primary and secondary are allowed
    if ($name == 'secondary' || $name == 'primary') {
        return $resource->getDb($name);
    } else {
        return $this->_db;
    }
}

function selectFromSecondary() {
    $db = $this->getAdapter('secondary');
    $select = $this->select(true);
    return $db->fetchAll($select); // normally this is $this->fetchAll()
}

Again, by overriding getAdapter() as shown above, you won't need to make any changes whatsoever if accessing the primary database, but if you need the secondary one you need to obtain the secondary adapter via $this->getAdapter('secondary') and store it into a variable, i.e. $db, and then call select/insert/update/delete methods using the $db object.

EDIT Slight modification to code above. You should try to utilize $this->_db by default for getAdapter() and the $db-> replaces $this-> for fetch(), update(), insert(), delete(), etc, not for select().


function getAdapter($name = 'primary') {
    $resource = $this->getPluginResource('multidb');
    $resource->init();

    // Ensure only primary and secondary are allowed
    if ($name == 'secondary' || $name == 'primary') {
        return $resource->getDb($name);
    } else {
        return $this->_db;
    }
}

function selectFromSecondary() {
    $db = $this->getAdapter('secondary');
    $select = $this->select(true);
    return $db->fetchAll($select); // normally this is $this->fetchAll()
}
0

精彩评论

暂无评论...
验证码 换一张
取 消