Symfony2 - DoctrineMongoDBBundle - Doctrine\Common\Annotations\AnnotationException

发布于 2024-11-24 07:43:04 字数 1156 浏览 3 评论 0原文

我正在尝试使用 DoctrineMongoDBBundle,但是,我遇到了问题。

在我的 config.yml 中,我有:

doctrine_mongodb:
    connections:
        default:
            server: mongodb://localhost:27017
            options:
                connect: true
    default_database: symfony2
    document_managers:
        default:
            auto_mapping: true

我的 User.php 类:

<?php
namespace HALL\HelloWorldBundle\Document;
use FOS\UserBundle\Document\User as BaseUser;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class User extends BaseUser
{
    /** @MongoDB\Id(strategy="auto") */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

当我运行命令时:

 php app/console doctrine:mongodb:generate:documents HALLHelloWorldBundle

我收到以下错误:

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

有什么想法吗?注释已明确引用。

I'm trying to use the DoctrineMongoDBBundle, however, i'm running into an issue.

In my config.yml, I have:

doctrine_mongodb:
    connections:
        default:
            server: mongodb://localhost:27017
            options:
                connect: true
    default_database: symfony2
    document_managers:
        default:
            auto_mapping: true

My User.php class:

<?php
namespace HALL\HelloWorldBundle\Document;
use FOS\UserBundle\Document\User as BaseUser;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class User extends BaseUser
{
    /** @MongoDB\Id(strategy="auto") */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

When I run the command:

 php app/console doctrine:mongodb:generate:documents HALLHelloWorldBundle

I get the following error:

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

Any ideas why? The annotation is clearly referenced.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贱贱哒 2024-12-01 07:43:04

找到解决方案。

<一href="http://groups.google.com/group/symfony2/browse_thread/thread/0d45a6bfe4b04ee7/645f347c77bdc3e6?show_docid=645f347c77bdc3e6 ">http://groups.google.com/group/symfony2/browse_thread/thread/0d45a6bfe4b04ee7/645f347c77bdc3e6?show_docid=645f347c77bdc3e6

在 app/autoload.php 中,我需要添加:

Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::registerAnnotationClasses(); 

啊,我希望文档能告诉我这一点......

Solution found.

http://groups.google.com/group/symfony2/browse_thread/thread/0d45a6bfe4b04ee7/645f347c77bdc3e6?show_docid=645f347c77bdc3e6

in app/autoload.php, I needed to add:

Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::registerAnnotationClasses(); 

Ah, I wish the documentation would tell me this....

醉南桥 2024-12-01 07:43:04

像 Jamie 的解决方案中那样注册注释对我来说不起作用。它解决了这个问题,但意味着注释对象无法从缓存中反序列化。像这样注册注解:

AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php');

意味着原来的问题已经解决,没有引入与缓存相关的问题。

Registering the annotations as in Jamie's solution did not work for me. It solved this problem but meant that the annotations object could not be unserialized from the cache . Registering the annotations like this:

AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php');

Meant that the original issue was resolved without introducing the issue relating to the cache.

心碎的声音 2024-12-01 07:43:04

您应该在引导程序上注册注释类,这可以通过两种方式完成。使用 Richard 详细介绍的静态调用。或者...

您可以在驱动程序对象上使用 registerAnnotationClasses() 方法。这应该做完全相同的事情,但不需要路径参数(因为在引导程序上设置驱动程序时应该已经给出了路径参数)。

use \Doctrine\ODM\MongoDB\Configuration;

.........

$configuration = new Configuration();
$driver = $configuration->newDefaultAnnotationDriver($path_to_docs);
$driver->registerAnnotationClasses();

You should register the annotation classes on bootstrap, this can be done in 2 ways. Using the static call as detailed by Richard. Or...

You can use the registerAnnotationClasses() method on your driver object. This should do exactly the same thing but doesn't require a path parameter (as it should have already been given when setting up your driver on bootstrap).

use \Doctrine\ODM\MongoDB\Configuration;

.........

$configuration = new Configuration();
$driver = $configuration->newDefaultAnnotationDriver($path_to_docs);
$driver->registerAnnotationClasses();
失去的东西太少 2024-12-01 07:43:04

DoctrineMongoDBBundle 文档中找到的解决方案< /a>

你的 app/autoload.php 必须是这样的:

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; <-- add this line 

$loader = require __DIR__.'/../vendor/autoload.php';

if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

    $loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

AnnotationDriver::registerAnnotationClasses();  <-- add this line

return $loader;

Solution found in Documentation of DoctrineMongoDBBundle

your app/autoload.php must be like this :

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; <-- add this line 

$loader = require __DIR__.'/../vendor/autoload.php';

if (!function_exists('intl_get_error_code')) {
    require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';

    $loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

AnnotationDriver::registerAnnotationClasses();  <-- add this line

return $loader;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文