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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Doctrine2更新导致Zend Framework 3中的AnnotationRegistry registerLoader错误相关的知识,希望对你有一定的参考价值。

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

致命错误:未捕获TypeError: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 M) oduleManager ModuleEvent))#4 / home / p in /home/platne/serwer18346/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php on 117

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

return [
    'ZendRouter',
    'ZendValidator',
    'DoctrineModule',
    'DoctrineORMModule',
    'Core',
];

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

'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => DoctrineDBALDriverPDOmysqlDriver::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'
                ]
            ]
        ]
    ]

控制器工厂:

public function __invoke(ContainerInterface $container,$requestedName, array $options = null)
{
    $controllerInstance = null;
    switch($requestedName){
        case 'CoreControllerIndexController': $controllerInstance = $this->_invokeIndex($container); break;
        case 'CoreControllerPagesController': $controllerInstance = $this->_invokePages($container); break;
    }
    return $controllerInstance;
}

protected function _invokeIndex(ContainerInterface $container)
{
    return new ControllerIndexController(
        $container->get('doctrine.entitymanager.orm_default')
    );
}

protected function _invokePages(ContainerInterface $container)
{
    return new ControllerPagesController(
        $container->get('doctrine.entitymanager.orm_default')
    );
}

控制器父母:

 protected $_entityManager;

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

    /**
     * @return EntityManager
     */
    public function getEntityManager()
    {
        return $this->_entityManager;
    }

正如我所说,此代码在更新之前有效。更新后,它向我显示该错误,上传以前的版本后错误仍然存​​在。我triead重写代码但具有相同的效果。

Composer(没有项目数据):

"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/"
    }
  }
答案

由最新版本的DoctrineCommonAnnotations引起的此错误使用PHP 7.1。这就是为什么它使用void作为return type。它在PHP 7.0上不受支持。*。这是new feature in PHP 7.1

我使用PHP 7.0在我的ZF3项目中使用doctrine-orm-module 1.1。它运作良好。所以,只需将你的doctrine-orm-module版本替换为1.1

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

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

另一答案

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

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

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

另一答案

您可以尝试使用以下配置。这个对我有用。

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

报告作曲家/包问题时,也非常有用,是作曲家节目的输出。我看起来像这样:

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

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

例如,教义/注释:https://packagist.org/packages/doctrine/annotations#v1.5.0

然后查找(需要:php:^ 7.1)并且此包是否与您的PHP版本匹配。 (在你的情况下使用PHP 7.0它不匹配)

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

另一答案

只需删除项目中的composer.lock,同样删除“vendor”文件夹。

运行并享受 - >

php composer.phar selfupdate

php composer.phar安装

以上是关于Doctrine2更新导致Zend Framework 3中的AnnotationRegistry registerLoader错误的主要内容,如果未能解决你的问题,请参考以下文章

如何避免与 Doctrine2 和 Zend Framework 2 的多对多关系重复?

Zend Framework 2 + Doctrine 2 [关闭]

在 Zend 框架 2 中使用 MappedSuperclass 的 Doctrine 2 多对多

是否可以将 Doctrine 2 与 Zend Framework 1.1x 集成?

zend 框架 2 + 教义 2 安装

Zend Framework 和 Doctrine 2 - 我的单元测试是不是足够?