romainneutron / MongoDB-ODM-Silex-Provider

This project is not maintained anymore, contact me to take over it
Other
35 stars 15 forks source link

Error on autoloading annotations #3

Closed titomiguelcosta closed 11 years ago

titomiguelcosta commented 11 years ago

I only managed to get this service to work in my Silex project when I added the annotation autoloader:

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

Before, I was getting the following error:

AnnotationException: [Semantical Error] The annotation   

"@Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class     

TitoMiguelCosta\Document\User does not exist, or could not be auto-loaded.

Not sure if I'm doing something wrong or this the autoloading for annotation is missing.

Although we have the official documentation, It would be nice to have an example of a Document.

romainneutron commented 11 years ago

I don't use annotation, @mavimo may know more about this than me, any idea ?

But, after reading doctrine code, it seems ok for me. Maybe we could registerAnnotationClasses() in case user wants to use annotation driver

mavimo commented 11 years ago

In accord to documentation you need to register your documents autoloading, eg:

// Configure MongoDB service provider
$app->register(new MongoDBODMServiceProvider(), array(
    'doctrine.odm.mongodb.connection_options' => array(
        'database' => $_SERVER['MONGODB_DB'],
        'host'     => $_SERVER['MONGODB_SERVER'],
        'port'     => $_SERVER['MONGODB_PORT'],
    ),
    'doctrine.odm.mongodb.documents' => array(
        0 => array(
            'type' => 'annotation',
            'path' => array(
                __DIR__ . '/Mavimo',
            ),
            'namespace' => 'Mavimo'
        ),
    ),
    // 'doctrine.odm.mongodb.documents' => array(),
    'doctrine.odm.mongodb.proxies_dir'           => DIR_CACHE . '/doctrine/odm/mongodb/Proxy',
    'doctrine.odm.mongodb.proxies_namespace'     => 'DoctrineMongoDBProxy',
    'doctrine.odm.mongodb.auto_generate_proxies' => true,
    'doctrine.odm.mongodb.hydrators_dir'         => DIR_CACHE . '/doctrine/odm/mongodb/Hydrator',
    'doctrine.odm.mongodb.hydrators_namespace'   => 'DoctrineMongoDBHydrator',
    'doctrine.odm.mongodb.metadata_cache'        => new Doctrine\Common\Cache\ApcCache(),
));

In document class:

namespace Mavimo\Entity;

use Mavimo\Entity\Base;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ORM;

/**
 * @ORM\Document(collection="events")
 */
class Event extends Base
{
    /**
     * @ORM\Id
     */
    private $id;

    /**
     * @ORM\String
     */
    private $title;

    // Add more...
}

in your code you use @Doctrine are you sure you need to have @ in your code?

lucianodelucchi commented 11 years ago

While trying to run the odm:generate:documents command I encountered the same error as @titomiguelcosta. After adding the

AnnotationDriver::registerAnnotationClasses();

the command run successfully. I'll send a pull request with the fix I used.

romainneutron commented 11 years ago

Yes, a PR is wecome ! thanks !

romainneutron commented 11 years ago

Fixed in #6 , available in tag 0.1.1