1. update global.php
return array( 'db' => array( 'adapters' => array( 'db' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=db1;host=localhost', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ), ), 'db_securitas' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=db2;host=localhost', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ), ), ), ), 'service_manager' => array( 'abstract_factories' => array( 'Zend\Db\Adapter\AdapterAbstractServiceFactory', ), ), );2. Update local.php
return array( 'db' => array( 'adapters' => array( 'db1' => array( 'username' => 'user', 'password' => 'pwd', ), 'db2' => array( 'username' => 'user', 'password' => 'pwd', ), ), ), );3. Use adapter in Module.php of your module:
// Add this method: public function getServiceConfig() { return array( 'factories' => array( 'ModuleABC\Model\ABCTable' => function($sm) { $tableGateway = $sm->get('ABCTableGateway'); $table = new ClientTable($tableGateway); return $table; }, 'ABCTableGateway' => function ($sm) { $dbAdapter = $sm->get('db1'); $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new ABC()); return new TableGateway('abc', $dbAdapter, null, $resultSetPrototype); }, 'ModuleXYZ\Model\XYZTable' => function($sm) { $tableGateway = $sm->get('XYZTableGateway'); $table = new BranchTable($tableGateway); return $table; }, 'XYZTableGateway' => function ($sm) { $dbAdapter = $sm->get('db2'); $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new XYZ()); return new TableGateway('xyz', $dbAdapter, null, $resultSetPrototype); }, ), ); }4. Done!
No comments:
Post a Comment