Zend framework2 在ZF2上配置多数据库连接
我实际上是ZF2的初学者 我成功地在同一个应用程序上使用了多个BDD,而且效果很好。 (我说的是:) 不过,我有个小问题 可以在global.php中声明我的自定义工厂吗?(在服务经理的事情上)。 或者我需要在每个模块中声明它吗?(在module.php中) 将其声明为global.php实际上是可行的,但我想知道这是否破坏了框架的精神或其他东西 谢谢你的时间Zend framework2 在ZF2上配置多数据库连接,zend-framework2,Zend Framework2,我实际上是ZF2的初学者 我成功地在同一个应用程序上使用了多个BDD,而且效果很好。 (我说的是:) 不过,我有个小问题 可以在global.php中声明我的自定义工厂吗?(在服务经理的事情上)。 或者我需要在每个模块中声明它吗?(在module.php中) 将其声明为global.php实际上是可行的,但我想知道这是否破坏了框架的精神或其他东西 谢谢你的时间 TONU将您的连接设置存储在本地配置中: config/autoload/local.php 在这种情况下,您有多个具有不同数据库/连
TONU将您的连接设置存储在本地配置中:
config/autoload/local.php
在这种情况下,您有多个具有不同数据库/连接凭据等的环境。例如,您可以使用单独的数据库进行临时设置和实时设置。
然后,您也可以通过这种方式在应用程序中使用多个连接
没有什么可以阻止您在这里设置多个连接,并根据需要在数据库适配器等中使用它们
local.php
return array(
/**
* Database Connection One
*/
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=dbnamehere;host=localhost',
'username' => 'root',
'password' => '',
),
/**
* Database Connection Two
*/
'db_two' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=anotherdb;host=localhost',
'username' => 'root',
'password' => '',
),
return array(
/**
* Database Adapter(s)
*/
'service_manager' => array(
'factories' => array(
/**
* Adapter One - this factory will use the default 'db' connection
*/
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
/**
* Adapter Two - use the second connection
*/
'Application\Db\AdapterTwo' => function($sm) {
$config = $sm->get('Config');
return new Adapter($config['db_two']);
},
),
),
);
如果您使用的是版本控制(应该是!),这还允许您从存储库中排除.local config文件,以避免在其中存储密码等,并允许更轻松地部署到多个环境
您也可以设置多个适配器以使用不同的连接:
global.php
return array(
/**
* Database Connection One
*/
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=dbnamehere;host=localhost',
'username' => 'root',
'password' => '',
),
/**
* Database Connection Two
*/
'db_two' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=anotherdb;host=localhost',
'username' => 'root',
'password' => '',
),
return array(
/**
* Database Adapter(s)
*/
'service_manager' => array(
'factories' => array(
/**
* Adapter One - this factory will use the default 'db' connection
*/
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
/**
* Adapter Two - use the second connection
*/
'Application\Db\AdapterTwo' => function($sm) {
$config = $sm->get('Config');
return new Adapter($config['db_two']);
},
),
),
);
要一次连接多个数据库,请执行以下步骤: 第1步: 创建/module/MyModule/并添加到application.config.ini以访问 第二步: 使用以下脚本在/Module/MyModule/目录中创建Module.php
<?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'),
),
);
}
}
第五步:
将方法添加到控制器中以访问表,如下所示:
在课程开始时声明:
受保护的$this->yourTable
public function getYourTable()
{
if (!$this->yourTable) {
$sm = $this->getServiceLocator();
$this->yourTable = $sm->get('YourModule\Model\YourTable');
}
return $this->yourTable;
}
然后,您可以在控制器中使用此函数(getYourTable())调用模型方法进行选择、更新和插入。小编辑——在global.php中,我必须向类添加路径:returnnew Zend\Db\Adapter\Adapter($config['Db_two'])代码>