【问题标题】:ZF2 + Doctrine multiple connections issueZF2 + Doctrine 多连接问题
【发布时间】:2015-12-06 22:57:11
【问题描述】:

我有以下情况:

一个 ZF2 驱动的应用程序,它使用 Doctrine 来处理与数据库相关的东西。 我的目录布局是一个典型的 ZF2 应用程序,除了我的模块目录(包含 src 的那个)有一个子目录 test 和一个单独的模块,用于测试父文件夹中的模块。

├───MyModule
│   ├───config
│   ├───src
│   │   └───MyModule
│   │       ├───Controller
│   │       ├───DoctrineExtensions
│   │       ├───Entity
│   │       ├───Factory
│   │       │   └───ViewHelper
│   │       ├───Fieldset
│   │       ├───Filter
│   │       ├───Form
│   │       │   └───Element
│   │       ├───Helper
│   │       ├───Hydrator
│   │       │   └───Strategy
│   │       ├───Mail
│   │       ├───Model
│   │       ├───Service
│   │       ├───Traits
│   │       ├───Validator
│   │       └───View
│   │           └───Helper
│   ├───test
│   │   ├───doc
│   │   └───MyModuleTest
│   │       ├───data
│   │       │   ├───DoctrineORMModule
│   │       │   │   └───Proxy
│   │       │   └───sql
│   │       │       └───data_single
│   │       ├───Fixture
│   │       └───Service
│   └───view
│       ├───flash-messenger
│       └───partial

我想使用单独的 Doctrine 配置和数据库连接,所以我根据 DoctrineORMModule 文档进行设置。

在Bootstrap.php 文件中,我用于设置加载被测模块(在每次测试运行之前由 PHPUnit 加载)我设置 ZF2 的 ServiceManager 并加载 Doctrine 配置。

static::$config = $config; // contains merged config
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->setFactory('ServiceListener', 'Zend\Mvc\Service\ServiceListenerFactory');

但是当我尝试获取实体管理器时

$em = $serviceManager->get('doctrine.entitymanager.orm_testing_mysql');

我总是得到一个

RuntimeException:在“doctrine.entitymanager”中找不到名为“orm_testing_mysql”的选项

运行我的调试器,我发现$serviceManage->instances['applicationconfig'] 包含我在配置文件中定义的所有数组。 $serviceManage->instances['config']['doctrine'] 但是只包含模块附带的默认原则配置/连接数据。

有人能弄清楚出了什么问题吗? 任何帮助将不胜感激..

【问题讨论】:

    标签: php zend-framework doctrine-orm zend-framework2 phpunit


    【解决方案1】:
         use Zend\ServiceManager\ServiceManager;
            use Zend\Mvc\Service\ServiceManagerConfiguration;
    
            class Bootstrap
    
        {
            protected static $application;
    
            public static function init()
            {
    
                chdir(dirname(__DIR__ . '/../../../../../')); // <-Project path
    
                include __DIR__ . '/../../../../init_autoloader.php'; //<- ur init_autoloader.php
    
                self::$application = \Zend\Mvc\Application::init(include 'config/application.config.php');
            }
    
            public static function getServiceManager()
            {
                return self::$application->getServiceManager();
            }
            public static function getEntityManager()
            {
            return self::getServiceManager()->get('doctrine.entitymanager.orm_default');
            }
            }
    

    那么当你使用 in 打开与 ORM 的连接时

    setUp()
    {
        $this->service = Bootstrap::getServiceManager()->get('ServiceRoute')
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多