एक समय में एकाधिक डेटाबेस से कनेक्ट करने के लिए, निम्न चरणों का पालन करें:
चरण 1 :
बनाएँ/मॉड्यूल/MyModule/और जोड़ें access.config.ini में पहुंचने के लिए।
चरण 2: Module.php बनाएं/मॉड्यूल/MyModule/निम्न स्क्रिप्ट के साथ निर्देशिका में
<?php
namespace MyModule;
use MyModule\MyAdapterFactory;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
class Module implements ServiceProviderInterface{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__.'/Db/Adapter/',
),
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'adapter1' => new MyAdapterFactory('db_adapter1'),
'adapter2' => new MyAdapterFactory('db_adapter2'),
),
);
}
}
चरण 3:
रास्ते में MyAdapterFactory.php बनाएँ:/मॉड्यूल/MyModule/src/MyModule/डीबी/एडाप्टर/निम्न स्क्रिप्ट के साथ।
<?php
namespace MyModule;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\Adapter\Adapter;
class MyAdapterFactory implements FactoryInterface
{
protected $configKey;
public function __construct($key)
{
$this->configKey = $key;
}
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config');
return new Adapter($config[$this->configKey]);
}
}
?>
चरण 4:
अपने getServiceConfig में निम्न स्क्रिप्ट जोड़ें()।
'YourModule\Model\YourTable' => function($sm) {
$tableGateway = $sm->get('YourTableGateway');
$table = new YourTable($tableGateway);
return $table;
},
'YourTableGateway' => function ($sm) {
$adapter1 = $sm->get('adapter1');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new YourModel());
return new TableGateway('tbl_name', $adapter1, null, $resultSetPrototype);
},
चरण 5:
जोड़ें अपने नियंत्रक में विधि के रूप में नीचे अपनी मेज तक पहुँचने के लिए:
वर्ग की शुरुआत पर इस घोषित:
$ इस- संरक्षित> yourTable ;
public function getYourTable()
{
if (!$this->yourTable) {
$sm = $this->getServiceLocator();
$this->yourTable = $sm->get('YourModule\Model\YourTable');
}
return $this->yourTable;
}
फिर, आप अपने नियंत्रक में इस समारोह (getYourTable()) का उपयोग करें, अद्यतन, सम्मिलित करें के लिए अपने मॉडल तरीकों कॉल कर सकते हैं।
धन्यवाद, इससे मदद मिली! – Tounu
छोटे संपादन - global.php में मुझे कक्षा में पथ जोड़ना होगा: 'नया ज़ेंड \ डीबी \ एडाप्टर \ एडाप्टर ($ config [' db_two '] वापस लौटें); ' – user2047861