当我尝试在ZF2中使用doctrine 2时,我收到以下错误。
Zend \ ServiceManager \ ServiceManager :: get无法为doctrine.entitymanager.orm_default提取或创建实例
包含并激活所需的模块。
doctrine.config.local.php
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => [
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'password',
'dbname' => 'database',
'charset' => 'utf8',
'driverOptions' => [
1002 => 'SET NAMES utf8'
],
],
],
],
'configuration' => [
'orm_default' => [
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'generate_proxies' => false,
]
]
]
];
核心模块的module.config.php (/module/Core/config/module.config.php)
namespace Core;
return [
'service_manager' => include __DIR__ . '/service-manager.config.php',
'router' => include __DIR__ . '/router.config.php',
'view_manager' => include __DIR__ . '/view-manager.config.php',
'translator' => include __DIR__ . '/translator.config.php',
'hydrators' => include __DIR__ . '/hydrators.config.php'
];
核心模块的doctrine.config.php (/module/Core/config/doctrine.config.php)
return [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => [
__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity',
]
],
'orm_default' => [
'drivers' => [
__NAMESPACE__ => __NAMESPACE__ . '_driver'
]
]
]
];
工厂
namespace Event\Form\Fieldset\Factory;
use Event\Entity\Event;
use Event\Form\Fieldset\EventFieldset;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class EventFieldsetFactory implements FactoryInterface {
public function createService(ServiceLocatorInterface $formElementManager) {
$serviceLocator = $formElementManager->getServiceLocator();
$hydratorManager = $serviceLocator->get('HydratorManager');
// Here I get the error!
$objectManager = $serviceLocator->get('Doctrine\ORM\EntityManager');
$fieldset = new EventFieldset();
$fieldset->setObjectManager($objectManager);
$fieldset->setHydrator($hydratorManager->get('Core\Hydrator\Doctrine'));
$fieldset->setObject(new Event());
return $fieldset;
}
}
application.config.php 中的modules
'modules' => [
'DoctrineModule',
'DoctrineORMModule',
'Core',
'Event',
'Inquiry'
]
堆栈跟踪
Zend\ServiceManager\Exception\ServiceNotFoundException
文件:
/src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php:555
消息:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for doctrine.entitymanager.orm_default
堆栈追踪:
#0 /src/vendor/doctrine/doctrine-orm-module/src/DoctrineORMModule/Service/EntityManagerAliasCompatFactory.php(44): Zend\ServiceManager\ServiceManager->get('doctrine.entity...')
#1 [internal function]: DoctrineORMModule\Service\EntityManagerAliasCompatFactory->createService(Object(Zend\ServiceManager\ServiceManager), 'doctrineormenti...', 'Doctrine\ORM\En...')
#2 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(934): call_user_func(Array, Object(Zend\ServiceManager\ServiceManager), 'doctrineormenti...', 'Doctrine\ORM\En...')
#3 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(1092): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Array, 'doctrineormenti...', 'Doctrine\ORM\En...')
#4 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(634): Zend\ServiceManager\ServiceManager->createFromFactory('doctrineormenti...', 'Doctrine\ORM\En...')
#5 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(597): Zend\ServiceManager\ServiceManager->doCreate('Doctrine\ORM\En...', 'doctrineormenti...')
#6 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(530): Zend\ServiceManager\ServiceManager->create(Array)
#7 /src/module/Event/src/Event/Form/Fieldset/Factory/EventFieldsetFactory.php(22): Zend\ServiceManager\ServiceManager->get('Doctrine\ORM\En...')
#8 [internal function]: Event\Form\Fieldset\Factory\EventFieldsetFactory->createService(Object(Zend\Form\FormElementManager\FormElementManagerV2Polyfill), 'eventfieldsetev...', 'Event\Fieldset\...')
#9 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(934): call_user_func(Array, Object(Zend\Form\FormElementManager\FormElementManagerV2Polyfill), 'eventfieldsetev...', 'Event\Fieldset\...')
#10 /src/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(330): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Array, 'eventfieldsetev...', 'Event\Fieldset\...')
#11 /src/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(287): Zend\ServiceManager\AbstractPluginManager->createServiceViaCallback(Array, 'eventfieldsetev...', 'Event\Fieldset\...')
#12 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(634): Zend\ServiceManager\AbstractPluginManager->createFromFactory('eventfieldsetev...', 'Event\Fieldset\...')
#13 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(597): Zend\ServiceManager\ServiceManager->doCreate('Event\Fieldset\...', 'eventfieldsetev...')
#14 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(530): Zend\ServiceManager\ServiceManager->create(Array)
#15 /src/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(161): Zend\ServiceManager\ServiceManager->get('Event\Fieldset\...', true)
#16 /src/vendor/zendframework/zend-form/src/FormElementManager/FormElementManagerTrait.php(38): Zend\ServiceManager\AbstractPluginManager->get('Event\Fieldset\...', Array, true)
#17 /src/vendor/zendframework/zend-form/src/Factory.php(111): Zend\Form\FormElementManager\FormElementManagerV2Polyfill->get('Event\Fieldset\...')
#18 /src/vendor/zendframework/zend-form/src/Form.php(176): Zend\Form\Factory->create(Array)
#19 /src/module/Inquiry/src/Inquiry/Form/InquiryForm.php(19): Zend\Form\Form->add(Array)
#20 /src/vendor/zendframework/zend-form/src/FormElementManager/FormElementManagerV2Polyfill.php(217): Inquiry\Form\InquiryForm->init()
#21 [internal function]: Zend\Form\FormElementManager\FormElementManagerV2Polyfill->callElementInit(Object(Inquiry\Form\InquiryForm), Object(Zend\Form\FormElementManager\FormElementManagerV2Polyfill))
#22 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(664): call_user_func(Array, Object(Inquiry\Form\InquiryForm), Object(Zend\Form\FormElementManager\FormElementManagerV2Polyfill))
#23 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(597): Zend\ServiceManager\ServiceManager->doCreate('Inquiry\Form\In...', 'inquiryforminqu...')
#24 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(530): Zend\ServiceManager\ServiceManager->create(Array)
#25 /src/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(161): Zend\ServiceManager\ServiceManager->get('Inquiry\Form\In...', true)
#26 /src/vendor/zendframework/zend-form/src/FormElementManager/FormElementManagerTrait.php(38): Zend\ServiceManager\AbstractPluginManager->get('Inquiry\Form\In...', Array, true)
#27 /src/module/Inquiry/src/Inquiry/Controller/Factory/InquiryControllerFactory.php(23): Zend\Form\FormElementManager\FormElementManagerV2Polyfill->get('Inquiry\Form\In...')
#28 [internal function]: Inquiry\Controller\Factory\InquiryControllerFactory->createService(Object(Zend\Mvc\Controller\ControllerManager), 'inquirycontroll...', 'Inquiry\Control...')
#29 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(934): call_user_func(Array, Object(Zend\Mvc\Controller\ControllerManager), 'inquirycontroll...', 'Inquiry\Control...')
#30 /src/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(330): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Array, 'inquirycontroll...', 'Inquiry\Control...')
#31 /src/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(287): Zend\ServiceManager\AbstractPluginManager->createServiceViaCallback(Array, 'inquirycontroll...', 'Inquiry\Control...')
#32 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(634): Zend\ServiceManager\AbstractPluginManager->createFromFactory('inquirycontroll...', 'Inquiry\Control...')
#33 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(597): Zend\ServiceManager\ServiceManager->doCreate('Inquiry\Control...', 'inquirycontroll...')
#34 /src/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(530): Zend\ServiceManager\ServiceManager->create(Array)
#35 /src/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php(161): Zend\ServiceManager\ServiceManager->get('Inquiry\Control...', true)
#36 /src/vendor/zendframework/zend-mvc/src/DispatchListener.php(94): Zend\ServiceManager\AbstractPluginManager->get('Inquiry\Control...')
#37 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#38 /src/vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#39 /src/vendor/zendframework/zend-eventmanager/src/EventManager.php(263): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#40 /src/vendor/zendframework/zend-mvc/src/Application.php(340): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#41 /src/public/index.php(15): Zend\Mvc\Application->run()
#42 {main}
答案 0 :(得分:3)
首先遇到的问题是配置加载程序。您已将其修改为包含:
'config_glob_paths' => [
'config/autoload/local/{,*.}local.php',
'config/autoload/global/{,*.}global.php'
],
但您的文件夹设置为:
配置/自动加载/全局/ doctrine.php
所以加载器不匹配。
此glob路径适用于您的文件结构:
'config_glob_paths' => [
'config/autoload/local/{,*.}php',
'config/autoload/global/{,*.}php'
],
作为替代方案,我上传了一个更“标准”的配置设置,可以帮助您:
https://github.com/Saeven/doctrine-error/tree/master/src/config
简而言之,您遇到了错误,因为无法建立连接,只是因为您的数据库详细信息不存在(因为您的配置加载程序已被清除)。
其次,看起来你的composer.json正在加载一些不兼容的项目。我在我维护的项目中使用了一个,并结合了加载器修复,当我输入有效的DB信用时,我看到“test”:
<强> composer.json 强>
{
"name": "test",
"minimum-stability": "dev",
"require": {
"php": ">=5.3.3",
"zendframework/zend-servicemanager": "@stable",
"zendframework/zend-eventmanager": "@stable",
"zendframework/zend-modulemanager": "@stable",
"zendframework/zend-developer-tools": "@stable",
"zendframework/zend-config": "@stable",
"zendframework/zend-console": "@stable",
"zendframework/zend-test": "@stable",
"zendframework/zend-crypt": "@stable",
"zendframework/zend-mail": "@stable",
"zendframework/zend-text": "@stable",
"zendframework/zend-serializer": "@stable",
"zendframework/zend-mvc": "2.5.3",
"zendframework/zend-filter": "@stable",
"zendframework/zend-db": "@stable",
"zendframework/zend-stdlib": "@stable",
"zendframework/zend-view": "@stable",
"zendframework/zend-form": "@stable",
"zendframework/zend-validator": "@stable",
"zendframework/zend-i18n": "@stable",
"zendframework/zend-log": "@stable",
"zendframework/zend-cache": "@stable",
"doctrine/doctrine-orm-module": "0.8.0",
"doctrine/orm": "2.5.*@dev"
}
}
答案 1 :(得分:0)
我认为您的问题出在您的工厂以及如何获得EntityManager。
以下是如何从ZF2工厂的Doctrine\ORM\EntityManager
获取serviceLocator
:
class YourFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
/* @var $em \Doctrine\ORM\EntityManager */
$em = $serviceLocator->getServiceLocator()->get('em');
// $em contains methods to get repo, etc ...
// injections here
$obj = new Obj();
$obj->setEntityManager($em);
return $obj;
}
}
试试并告诉我