【问题标题】:get doctrine db config in zf2在 zf2 中获取教义数据库配置
【发布时间】:2014-10-14 19:52:48
【问题描述】:

我想使用doctrine db config访问doctrine中的DBAL层,我的doctrine db文件配置中有以下配置: 数据库.local.php

return array(
  'doctrine' => array(
    'connection' => array(
      'orm_default' => array(
        'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
        'params' => array(
          'host'     => 'localhost',
          'port'     => '5432',
          'user'     => 'postgres',
          'password' => '123456',
          'dbname'   => 'test'
        )
      )
    )
  )
);

在我的控制器中 索引控制器.php

use Doctrine\DBAL\DriverManager;

public function testAction(){
   $conn = DriverManager::getConnection($params, $config);
}

我想在getConnection函数中使用上面的db config,可以吗?

【问题讨论】:

    标签: doctrine-orm zend-framework2 dbal


    【解决方案1】:

    如果您的local.php 中有一个数据库配置,那么为什么不通过EntityManager 访问它呢?就像这样:

    public function testAction(){
    
     $em = ->getServiceLocator()
           ->get('Doctrine\ORM\EntityManager');
    
     $conn = $em->getConnection();
    }
    

    如果你想通过DriverManager 得到它:

    $config = new \Doctrine\DBAL\Configuration();
    
    $connectionParams = array(
          'host'     => 'localhost',
          'port'     => '5432',
          'user'     => 'postgres',
          'password' => '123456',
          'dbname'   => 'test'
          'driver' => 'pdo_pgsql',
    );
    
    $conn = DriverManager::getConnection($connectionParams, $config);
    

    编辑:

    您还可以使用 Doctrine 提供的注册服务名称和您的实际数据库配置直接获取Doctrine\DBAL\Connection 的实例:

    $conn = $this->getServiceLocator()->get('doctrine.connection.orm_default');
    

    作为DriverManager::getConnection() 方法,这将返回一个Doctrine\DBAL\Connection,它包装了底层驱动程序连接。

    【讨论】:

    【解决方案2】:

    当然。

    首先你应该使用ServiceLocator

    ServiceLocator 会自动注入到实现 \Zend\ServiceManager\ServiceLocatorAwareInterface 的类中

    AbstractActionController,你的 zf2 控制器已经实现了这个接口。

    要在类中使用(通过示例建模),您应该声明实现和两个由接口设计的方法,setServiceLocator 和 getServiceLocator。

    <?php
        namespace Security\Repository;
    
        use Zend\ServiceManager\ServiceLocatorInterface;
        use Zend\ServiceManager\ServiceLocatorAwareInterface;
    
        class Repository implements ServiceLocatorAwareInterface
        {
            protected $serviceLocator;
    
            public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
            {
                $this->serviceLocator = $serviceLocator;
            }
    
            public function getServiceLocator()
            {
                return $this->serviceLocator;
            }
        }
    

    在 ZF2 上使用 ServiceLocator 很容易做任何事情。尝试了解如何获得 zf2 的全部功能。

    $configArray = $this->getServiceLocator()->get('config');
    
    $config = new \Doctrine\DBAL\Configuration();
    
    $connectionParams = $configArray['doctrine']['connection']['orm_default']['params']
    
    $conn = DriverManager::getConnection($connectionParams, $config);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多