Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - DoctrineMongoDBBundle - Doctrine\Common\Annotations\AnnotationException

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.

like image 721
Flukey Avatar asked Jul 15 '11 14:07

Flukey


3 Answers

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

like image 100
Flukey Avatar answered Nov 19 '22 17:11

Flukey


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.

like image 4
Richard Miller Avatar answered Nov 19 '22 17:11

Richard Miller


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();
like image 1
user1252267 Avatar answered Nov 19 '22 15:11

user1252267