学说 odm 注释或作曲家 autoload.php 不起作用?

doctrine odm annotations or composer autoload.php not working?

我正在尝试在 Yii2 框架的项目上使用 Doctrine MongoDB ODM 2.0 beta,composer 版本 1.8.4 和 PHP 7.2,但我不断收到错误 Fatal error: Uncaught Error: Call to a member function add() on boolean 代码运行的地方 $loader->add('Documents', __DIR__);

bootstrap.php 文件(在 DIR/bootstrap.php):

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;

if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
    throw new RuntimeException('Install dependencies to run this script.');
}

$loader = require_once $file;
$loader->add('Documents', __DIR__);

AnnotationRegistry::registerLoader([$loader, 'loadClass']);

$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('fsa');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));

$dm = DocumentManager::create(null, $config);

我已经尝试查看 How to properly Autoload Doctrine ODM annotations? and Laravel & Couchdb-ODM - The annotation "@Doctrine\ODM\CouchDB\Mapping\Annotations\Document" does not exist, or could not be auto-loaded 和许多我不太记得的其他线程来寻求帮助,但我无法找到解决方案。

我也试过注释掉下面的行

if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
    throw new RuntimeException('Install dependencies to run this script.');
}

$loader = require_once $file;
$loader->add('Documents', __DIR__);

AnnotationRegistry::registerLoader([$loader, 'loadClass']);

和 运行 composer dump-autoload 并在命令行上返回 Generated autoload files containing 544 classes,但后来我遇到了问题

[Semantical Error] The annotation "@Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Documents\Message does not exist, or could not be auto-loaded.

所以注释不是自动加载的,我不知道如何解决这个问题。 在我的模型中:

<?php

namespace Documents;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use \Doctrine\ODM\MongoDB\Mapping\Annotations\Document;

/** @ODM\Document */
class Message
{
    /** @ODM\Id */
    private $id;

    /** @ODM\Field(type="int") */
    private $sender_id;
...

我还在 https://github.com/doctrine/mongodb-odm/issues/1976 post 在 github 上发了一个帖子。一位评论者说 "By default, the composer autoload file returns the autoloader in question, which seems to not be the case for you." 我该如何解决?我能在网上找到的唯一信息是(在 composer.json 内)这些行:

    "autoload": {
        "psr-4": {
            "Class\": "src/"
        }
    },

但是 class 我应该加载什么?

我很困惑,对所有这些工具(mongodb、yii2 等)都很陌生,一点帮助也没有。我不确定还有哪些其他信息会有帮助,否则我会 post 它。

提前致谢。

结果是问题(如 https://github.com/doctrine/mongodb-odm/issues/1976 中提到的)是 autoload.php 被要求两次 - 一次在 bootstrap.php 中,一次在 web/index.php 中(的框架)。删除 index.php 中的 require 行后,一切正常。