php Doctrine2 更新导致 Zend Framework 3 中的 AnnotationRegistry registerLoader 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45337962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:06:22  来源:igfitidea点击:

Doctrine2 Update Caused AnnotationRegistry registerLoader Error in Zend Framework 3

phpzend-frameworkdoctrine-ormzend-framework3

提问by Manveru

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:

我正在开发基于 Zend Framework 3.0 的 CMS,以使用 Doctrine 管理 DB I。使用 Composer 管理包时有什么问题?最近,我将所有软件包更新为最新版本并将其发送到服务器,其他文件中没有任何更改。更新后,我的网站显示以下错误:

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

致命错误:未捕获的类型错误:Doctrine\Common\Annotations\AnnotationRegistry::registerLoader() 的返回值必须是 Doctrine\Common\Annotations\void 的一个实例,在 /home/platne/serwer18346/vendor/doctrine/annotations/ 中没有返回lib/Doctrine/Common/Annotations/AnnotationRegistry.php:117 堆栈跟踪:#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 第 117 行

Some application code if needed:
modules:

如果需要,一些应用程序代码:
模块:

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

development.local(developer mode is active):

development.local(开发者模式处于活动状态):

'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/"
    }
  }

回答by Dolly Aswin

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

这个错误是Doctrine\Common\Annotations使用最新版本的PHP 7.1 引起的。这就是它使用voidas的原因return type。并且它在 PHP 7.0.* 上不受支持。这是PHP 7.1 的新特性

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

doctrine-orm-module 1.1在我的 ZF3 项目中使用 PHP 7.0。它运作良好。因此,只需将您的doctrine-orm-module版本替换为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.

我建议你定义你在 composer.json 中使用的依赖项的版本。这是为了让你的项目在新版本的依赖发布时不会被破坏。

回答by Tokeeen.com

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

为了避免这种问题,一个好的做法是设置作曲家config.platform设置:

"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.

这将告诉作曲家更新软件包,但仅限于仍支持此 PHP 版本的版本。通常,此版本号将是您的生产服务器的版本。

回答by Dado

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:

当您报告 composer/package 问题时,也非常有用的是composer show的输出。我的看起来像这样:

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.

如果您以后遇到此类问题,请访问https://packagist.org/packages/并搜索导致问题的包。

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

例如学说/注释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)

然后在那里寻找 ( requires: php: ^7.1) 以及此包是否与您的 PHP 版本匹配。(在您使用 PHP 7.0 的情况下,它不匹配)

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

但是https://packagist.org/packages/doctrine/annotations#v1.4.0匹配您的 PHP 版本(需要:php:^5.6 || ^7.0),您可以尝试使用它。

回答by Djo Parasiite

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

只需删除项目中的 composer.lock,“供应商”文件夹也是如此。

run that and enjoy ->

运行它并享受 ->

php composer.phar selfupdate

php composer.phar 自我更新

php composer.phar install

php composer.phar 安装