Symfony2 - DoctrineMongoDBBundle - Doctrine\Common\Annotations\AnnotationException
Asked Answered
M

5

8

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.

Marquardt answered 15/7, 2011 at 14:43 Comment(0)
T
4

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.

Tobit answered 17/7, 2011 at 13:40 Comment(1)
In the end I used require_once __DIR__.'/../vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php'; Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::registerAnnotationClasses(); However, yours seems like a better solution! Accepted yours at the answer.Marquardt
M
9

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

Marquardt answered 15/7, 2011 at 15:7 Comment(1)
Then edit the doc and make a pull request (and SAVE THE WORLD) ;-)Locally
T
4

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.

Tobit answered 17/7, 2011 at 13:40 Comment(1)
In the end I used require_once __DIR__.'/../vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php'; Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::registerAnnotationClasses(); However, yours seems like a better solution! Accepted yours at the answer.Marquardt
K
1

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();
Kwarteng answered 8/3, 2012 at 10:12 Comment(0)
C
1

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;
Commutative answered 3/12, 2013 at 22:50 Comment(0)
T
0

http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

In the doc you can find this part of configuration

Turbulent answered 4/8, 2015 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.