How to properly Autoload Doctrine ODM annotations?

假如想象 提交于 2019-12-12 21:09:09

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/14569955/how-to-properly-autoload-doctrine-odm-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!