Doctrine2 Update Caused AnnotationRegistry registerLoader Error in Zend Framework 3

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I'm working on a CMS based on Zend Framework 3.0 to manage a DB I with Doctrine. What is my problem when managing packages with composer? Recently, I updated all the packages to newest versions and sent it to server, nothing was changed in other files. After the update my site displayed the following error:

Fatal error: Uncaught TypeError: Return value of Doctrine\Common\Annotations\AnnotationRegistry::registerLoader() must be an instance of Doctrine\Common\Annotations\void, none returned in /home/platne/serwer18346/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php:117 Stack trace: #0 /home/platne/serwer18346/vendor/doctrine/doctrine-module/src/DoctrineModule/Module.php(57): Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(Object(Closure)) #1 /home/platne/serwer18346/vendor/zendframework/zend-modulemanager/src/Listener/InitTrigger.php(33): DoctrineModule\Module->init(Object(Zend\ModuleManager\ModuleManager)) #2 /home/platne/serwer18346/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\ModuleManager\Listener\InitTrigger->__invoke(Object(Zend\ModuleManager\ModuleEvent)) #3 /home/platne/serwer18346/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): Zend\EventManager\EventManager->triggerListeners(Object(Zend\ModuleManager\ModuleEvent)) #4 /home/p in /home/platne/serwer18346/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php on line 117

Some application code if needed:
modules:

return [     'Zend\Router',     'Zend\Validator',     'DoctrineModule',     'DoctrineORMModule',     'Core', ]; 

development.local(developer mode is active):

'doctrine' => [         'connection' => [             'orm_default' => [                 'driverClass' => Doctrine\DBAL\Driver\PDOMySql\Driver::class,                 'params' => [                     'host' => '******',                     'user' => '*******',                     'password' => '******',                     'dbname' => '*******',                     'charset' => 'utf8'                 ]             ]         ]     ] 

module.config:

'doctrine' => [         'driver' => [             __NAMESPACE__ . '_driver' => [                 'class' => AnnotationDriver::class,                 'cache' => 'array',                 'paths' => [__DIR__.'/../src/Model']             ],             'orm_default' => [                 'drivers' => [                     __NAMESPACE__ . '\Model' => __NAMESPACE__ . '_driver'                 ]             ]         ]     ] 

Controller Factory:

public function __invoke(ContainerInterface $container,$requestedName, array $options = null) {     $controllerInstance = null;     switch($requestedName){         case 'Core\Controller\IndexController': $controllerInstance = $this->_invokeIndex($container); break;         case 'Core\Controller\PagesController': $controllerInstance = $this->_invokePages($container); break;     }     return $controllerInstance; }  protected function _invokeIndex(ContainerInterface $container) {     return new Controller\IndexController(         $container->get('doctrine.entitymanager.orm_default')     ); }  protected function _invokePages(ContainerInterface $container) {     return new Controller\PagesController(         $container->get('doctrine.entitymanager.orm_default')     ); } 

Controller Parent:

 protected $_entityManager;      /**      * AppController constructor.      * @param EntityManager $entityManager      */     public function __construct(EntityManager $entityManager)     {         $this->_entityManager = $entityManager;     }      /**      * @return EntityManager      */     public function getEntityManager()     {         return $this->_entityManager;     } 

As I said this code worked before update. After update it show me that error, what is more after uploading previous versions the error remains. I triead rewriting code but with the same effect.

Composer(without project data):

"require": {     "zendframework/zend-mvc": "*",     "zendframework/zend-developer-tools": "*",     "zendframework/zend-session": "*",     "zendframework/zend-authentication": "*",     "zfcampus/zf-development-mode": "*",     "doctrine/doctrine-orm-module": "*"   },   "autoload": {     "psr-4": {       "Core\\": "module/Core/src/"     }   } 

回答1:

This error caused by the latest version of Doctrine\Common\Annotations use PHP 7.1. That's why it use void as return type. And it is not supported on PHP 7.0.*. This is new feature in PHP 7.1

I use doctrine-orm-module 1.1 in my ZF3 project using PHP 7.0. And it work well. So, just replace your doctrine-orm-module version to 1.1.

"doctrine/doctrine-orm-module": "^1.1" 

I suggest you to define the version of dependencies you used in composer. This is purposed to make your project not broken when new version of dependencies released.



回答2:

To avoid this kind of problems, a good practice is to set the composer config.platform setting:

"config": {     "platform": {         "php": "7.0.23"     }  } 

This will tell composer to update packages but only to a version that still supports this PHP version. So typically, this version number will be the version of your production server.



回答3:

You can try to use the following configuration. It works for me.

    "require": {         "php": ">=5.5.9",         "doctrine/doctrine-bundle": "^1.6",         "doctrine/orm": "2.5.6",         "doctrine/annotations": "1.4.*",         "doctrine/dbal": "2.5.4",         ...      } 

Also very helpful when you reporting composer/package issues is the output of composer show. Mine looks like this:

doctrine/annotations                 v1.4.0  Docblock Annotations Parser doctrine/cache                       v1.7.0  Caching library offering an object-oriented API for many cache backends doctrine/collections                 v1.5.0  Collections Abstraction library doctrine/common                      v2.6.2  Common Library for Doctrine projects doctrine/dbal                        v2.5.4  Database Abstraction Layer doctrine/doctrine-bundle             1.6.8   Symfony DoctrineBundle doctrine/doctrine-cache-bundle       1.3.0   Symfony Bundle for Doctrine Cache doctrine/inflector                   v1.2.0  Common String Manipulations with regard to casing and singular/plural rules. doctrine/instantiator                1.0.5   A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer                       v1.0.1  Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers. doctrine/orm                         v2.5.6  Object-Relational-Mapper for PHP 

If you have such problems in the future, go to https://packagist.org/packages/ and search for package which causes problems.

For example doctrine/annotations: https://packagist.org/packages/doctrine/annotations#v1.5.0

Then look there for (requires: php: ^7.1) and if this package matches your PHP version. (In your case using PHP 7.0 it doesn't match)

But https://packagist.org/packages/doctrine/annotations#v1.4.0 matches your PHP version (requires: php: ^5.6 || ^7.0) and you can try to use it.



回答4:

Just delete composer.lock in your project, same for the "vendor" folder.

run that and enjoy ->

php composer.phar selfupdate

php composer.phar install



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!