zf2 doctrine2和Zend\Db\Adapter\Adapter使用一个db连接 [英] zf2 doctrine2 and Zend\Db\Adapter\Adapter using one db connection
问题描述
DoctrineORM
模块接受一个 PDO
资源或服务名称,实例可以位于服务管理器中,而不是通常的连接参数。
第一步是创建一个服务工厂,从 Zend\Db\Adapter\Adapter $ c中检索PDO资源$ c>服务
<?php
命名空间Application\Db\Service;
使用Zend\ServiceManager\FactoryInterface;
使用Zend\ServiceManager\ServiceLocatorInterface;
使用Zend\ServiceManager\Exception\ServiceNotCreatedException;
class PdoResourceFactory实现FactoryInterface
{
/ **
* @param ServiceLocatorInterface $ serviceLocator
* @return \PDO资源
* /
public function createService(ServiceLocatorInterface $ services)
{
$ dbAdapter = $ services-> get('Zend\Db\Adapter\Adapter');
$ pdo = $ dbAdapter-> getDriver() - > getConnection() - > getResource();
if(!$ pdo instanceof \PDO){
throw new ServiceNotCreatedException('Connection resource must be a instance of PDO');
}
return $ pdo;
}
}
一旦你有工厂,这只是一个将其添加到服务管理器中,为 Zend\Db\Adapter\Adapter
配置db参数,并告诉原则使用现有的 PdoResource
从服务管理器连接。
假设您在一个文件中完成所有操作,我们假设 dbconn.local.php
...
<?php
返回数组(
'service_manager'=>数组(
' '=> array(
'Zend\Db\Adapter\Adapter'=>'Zend\Db\Adapter\AdapterServiceFactory',
//包含pdo资源工厂
'PdoResource'=>'Application\Db\Service\PdoResourceFactory',
),
),
// db适配器配置
'db' =>数组(
'driver'=>'pdo',
'dsn'=>'mysql:dbname = database; host = 127.0.0.1',
'username' =''username',
'password'=>'password',
),
'doctrine'=>数组(
' =>数组(
'orm_default'=>数组(
'driverClass'=>'Doctrine\DBAL\Driver\PDOMySql\Driver',
//使用来自zend适配器的资源
'pdo'=> 'PdoResource',
),
),
),
);
I use doctrine2 with ZF2, some of my libraries work with Zend\Db\Adapter\Adapter, others with doctrine2. Now, they connect to database twice. Is it possible to use one db connection in doctrine and standard ZF2 db adapter?
The DoctrineORM
module accepts a PDO
resource or a service name where the instance can be located in the service manager instead of the usual connection params.
First step is to create a service factory which retrieves the PDO resource from the Zend\Db\Adapter\Adapter
service
<?php
namespace Application\Db\Service;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
class PdoResourceFactory implements FactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @return \PDO resource
*/
public function createService(ServiceLocatorInterface $services)
{
$dbAdapter = $services->get('Zend\Db\Adapter\Adapter');
$pdo = $dbAdapter->getDriver()->getConnection()->getResource();
if (!$pdo instanceof \PDO) {
throw new ServiceNotCreatedException('Connection resource must be an instance of PDO');
}
return $pdo;
}
}
Once you have the factory, it's just a case of adding it to the service manager, configuring the db params for Zend\Db\Adapter\Adapter
and telling doctrine to use the existing PdoResource
from the service manager to connect.
Assuming you did this all in one file, let's say dbconn.local.php
...
<?php
return array (
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
// include the pdo resource factory
'PdoResource' => 'Application\Db\Service\PdoResourceFactory',
),
),
// db adapter config
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=database;host=127.0.0.1',
'username' => 'username',
'password' => 'password',
),
'doctrine' => array (
'connection' => array (
'orm_default' => array (
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
// use the resource from the zend adapter
'pdo' => 'PdoResource',
),
),
),
);
这篇关于zf2 doctrine2和Zend\Db\Adapter\Adapter使用一个db连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!