MasterSlaveFeature How-To

2019-03-21 15:36发布

问题:

I currently have my Db Models using AbstractTableGateway and all my select/insert/update/delete queries are working just fine. But now I would like to add the MasterSlaveFeature and I'm a bit confused on how to do this. The documentation doesn't exactly give a good example:

http://zf2.readthedocs.org/en/latest/modules/zend.db.table-gateway.html#tablegateway-features

I currently have this setup:

namespace Login\Model;

use Zend\Db\TableGateway\Feature\MasterSlaveFeature;
use Zend\Db\TableGateway\Feature\FeatureSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Expression;

class Passport extends AbstractTableGateway
{
public function __construct($adapter, $slave)
{
    $this->table = 'passport';

    $this->adapter = $adapter;

    $this->featureSet = new FeatureSet();
    $this->featureSet->addFeature(new MasterSlaveFeature($slave));

    $this->initialize();
}

public function Profile($employeeid)
{
    $result = $this->select(function (Select $select) use ($employeeid) {
        $select
            ->columns(array(
                'count' => new Expression('COUNT(*)'),
                'employeeid',
                'passwd',
                'group',
                'name',
                'status',
                'timezone',
                'timeformat',
                'locale',
                'max_search'
            ))
            ->where($this->adapter->getPlatform()->quoteIdentifier('employeeid') . ' = ' . $this->adapter->getPlatform()->quoteValue($employeeid))
            ->limit(1);
    });

    return $result->current();
}

}

I am passing two Adapters $adapter and $slave, each have the same Tables, except one is empty and the other actually has data. Based on the documentation what I have should work, but I feel like I am missing something here, but don't know what.

The $adapter is the master database which has the data The $slave is the slave database which has no data

I would like my SELECTS to take the data from the $slave (which currently has no data) and Insert/Update/Delete should go to the $adapter.

Can someone help me figure this out? Thank you

回答1:

UPDATE: As of ZF 2.0.5 there is a bug. The configuration is correct.

UPDATE: Looks like this should be resolved in the 2.1 release

UPDATE: I can now confirm this is resolved and working in 2.0.7 and 2.1