2

Trying to run the command line tool w/ odm:schema:create and I'm getting errors like:

"[Semantical Error] The annotation "@Document" in class Company_Model_Auth was never imported. Did you maybe forget to add a "use" statement for this annotation?" and
"[Semantical Error] The annotation "@EmbeddedDocument" in class Company_Model_Auth was never imported. Did you maybe forget to add a "use" statement for this annotation?"

as well as others, basically for every annotation.

When I add "use \Doctrine\ODM\MongoDB\Mapping\Annotations\EmbeddedDocument;" (or \Document) to the file it works and progresses to the next Model. It will then complain on the next file about the same classes missing (Document / EmbeddedDocument and any other annotations) . Is it expected that I would have to add the use statements to every file?

Here is how I am building my DocumentManager::

public function _initDm() 
{
        AnnotationDriver::registerAnnotationClasses();
        $config = new Configuration();
        $config->setProxyDir(APPLICATION_PATH . '/../data/Proxies');
        $config->setProxyNamespace('Proxies');
        $config->setHydratorDir(APPLICATION_PATH . '/../data/Hydrators');
        $config->setHydratorNamespace('Hydrators');
        $config->setMetadataDriverImpl(AnnotationDriver::create(APPLICATION_PATH . '/models'));     

        // Pull in mongo db connection options from application.ini
        $options = $this->getOption('mongo');
        $config->setDefaultDB($options['database']);

        // Create a DocumentManager and store in ZendRegistry
        $dm = DocumentManager::create(new Connection($this->_createMongoDbConnectionString($options)), $config);
        Zend_Registry::set('dm', $dm); 
}

I double checked and the ./repos/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php file is definitely being hit and is require_once the proper annotation files.

Versions provisioned by composer:

"doctrine/common": "2.3.0-RC3",
"doctrine/mongodb": "1.0.1",
"doctrine/mongodb-odm": "1.0.0-BETA7",
"symfony/console": "2.1.*@dev",

Any help would be appreciated as I dont think I should have to add use statements to every file.

Mike Graf
  • 5,077
  • 4
  • 45
  • 58

1 Answers1

10

The annotation parser does require that the annotation classes are imported before use. In lieu of explicitly importing each annotation class, you can do something like the following (lifted from the test suite):

<?php

namespace Documents;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* @ODM\Document
*/
class User
{
    /** @ODM\Id */
    protected $id;

    /** @ODM\Field(type="string") */
    protected $username;

    // Other fields follow...
}
jmikola
  • 6,892
  • 1
  • 31
  • 61
  • I had already got a solution but forgot to post. You get the prize :) – Mike Graf Feb 01 '13 at 22:04
  • should I write this boilerplate every time? Or it's possible to set it somewhere in config? – OZ_ Jun 16 '13 at 19:17
  • 1
    If you're referring to the `use` statement as "boilerplate", the only alternative is to specify FQCNs in each annotation (e.g. `Doctrine\ODM\MongoDB\Mapping\Annotations\Id` and `Doctrine\ODM\MongoDB\Mapping\Annotations\Field`). The annotation parser requires that we have a resolvable class name for each annotation, and it supports aliases from `use` statements in a file. There is no config option to change this requirement. – jmikola Jun 24 '13 at 20:34
  • @MikeGraf, tell me what solution do you have? – Emil Sabitov Apr 24 '20 at 12:13
  • 1
    @EmilSabitov It was no better than the one here and was 7 yrs ago – Mike Graf Oct 21 '20 at 15:49
  • @MikeGraf, ha ha:) Ok – Emil Sabitov Oct 22 '20 at 17:09