【问题标题】:doctrine odm annotations or composer autoload.php not working?教义 odm 注释或作曲家 autoload.php 不起作用?
【发布时间】:2019-03-15 08:00:13
【问题描述】:

我正在尝试在带有 Yii2 框架的项目上使用 Doctrine MongoDB ODM 2.0 beta,作曲家版本为 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?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,但后来我遇到了问题

[语义错误] Documents\Message 类中的注释“@Doctrine\ODM\MongoDB\Mapping\Annotations\Document”不存在,或无法自动加载。

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

<?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;
...

我还在 github 上发布了一个帖子 https://github.com/doctrine/mongodb-odm/issues/1976。一位评论者表示:“默认情况下,composer 自动加载文件会返回有问题的自动加载器,但您似乎并非如此。”我该如何解决?我可以在网上找到的唯一信息是(在composer.json 内)以下行:

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

但是我应该加载什么类?

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

提前致谢。

【问题讨论】:

  • 您是否尝试运行composer dump-autoload(或类似的东西)?我记得有同样的错误,并重新转储自动加载文件。确保在运行 composer dump-autoload 之前更新/安装所有的 deps。
  • @Etshy 是的,我做到了,但我仍然得到Fatal error: Uncaught Error: Call to a member function add() on boolean 代码运行的地方$loader-&gt;add('Documents', __DIR__);
  • 很奇怪。你的 C:/path/to/vendor/autoload.php 里有什么?它应该包含另一个require autoload_real.php 并调用一个方法getLoader(),在你的情况下是否存在?
  • 是的,它包含autoload_real.php 文件和getLoader()。看起来像:require_once __DIR__ . '/composer/autoload_real.php'; return ComposerAutoloaderInit[longstring]::getLoader();
  • 是的,它似乎是正确的。奇怪的是,返回的$loader 是一个布尔值。 autoload_real.php 文件中有课程吗?在autoload_psr4.php 中是否列出了Annotation 命名空间?根据您的配置,它可以从 autoload_static.php 而不是 autoload_psr4.php 加载,但通常是 autoload_psr4.php

标签: php mongodb yii2 doctrine doctrine-odm


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-08-21
    • 2013-08-31
    • 1970-01-01
    • 2017-03-13
    • 2020-11-19
    • 2022-11-10
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多