1

The documentation mentions the ability to do master-slave by passing the MasterSlaveFeature to a TableGateway constructor along with the slave adapter as a parameter to the MasterSlaveFeature. My question is how to get access to that slave adapter when instantiating the TableGateway object.

I'm setting my master up in the main global and local configuration files, as per the quick start. How would I go about configuring the slave adapter(s) and how would I set up more than one slave?

Is there a working example of how to do this?

UPDATE: I've made some progress on this. In the global config file, I added another array key called slave and entered the connection information there. Then I created a SlaveAdapterServiceFactory class which mimics the behavior of Zend\Db\Adapter\AdapterServiceFactory but grabs the "slave" config key values instead of "db". Then, in my module config where I do the constructor injection of the master adapter, I also grab the slave adapter and pass it to my model table as the constructor parameter to a new MasterSlaveFeature instance. I also had to alter my model table class to look for Feature stuff getting passed in the constructor. Since the modal table class extends AbstractTableGateway I would expect it to know what to do with the Feature. Hopefully that assumption is correct. This all sounds really confusing! Here is some code. First the global.php from the config/autoload directory:

return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=zf2test;host=one.host.com',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'slave' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=zf2test;host=two.host.com',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                => 'Zend\Db\Adapter\AdapterServiceFactory',
            'My\Db\Adapter\SlaveAdapter'
                => 'My\Db\Adapter\SlaveAdapterServiceFactory'
        ),
    )    
);

Here is the code from the module's module.config.php file:

return array(
    'service_manager' => array(
        'factories' => array(
            'Album\Model\AlbumTable' =>  function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $slaveAdapter = $sm->get('My\Db\Adapter\SlaveAdapter');
                $table = new AlbumTable($dbAdapter, new MasterSlaveFeature($slaveAdapter));
                return $table;
            }
        )
    )
);

I haven't had a chance to test if the queries are actually going to the correct connections but I'm not getting any errors.

One other question is how to automatically pass these adapters to any models that get created instead of having to do this for each and every model that you instantiate.

GingerHead
  • 8,130
  • 15
  • 59
  • 93
Jeremy Hicks
  • 3,690
  • 5
  • 40
  • 52

1 Answers1

0

This should help you out: MasterSlaveFeature How-To

However talking with the current developer on this area of ZF2 there does appear to be a bug. I've filed a report based on his recommendation so once that is resolved, the example I have shown should work, combined with what you have as well =)

Community
  • 1
  • 1
Diemuzi
  • 3,507
  • 7
  • 36
  • 61