如何正确地自动加载Doctrine ODM注释? [英] How to properly Autoload Doctrine ODM annotations?

查看:110
本文介绍了如何正确地自动加载Doctrine ODM注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行带有odm:schema:create和的命令行工具 我遇到类似

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.

当我添加使用\ Doctrine \ ODM \ MongoDB \ Mapping \ Annotations \ EmbeddedDocument;"时(或\ Document)保存到它可以工作的文件,然后进入下一个模型.然后,它将在下一个文件中抱怨缺少相同的类(Document/EmbeddedDocument和任何其他注释).是否期望我必须在每个文件中添加use语句?

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?

这是我构建DocumentManager的方式:

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); 
}

我仔细检查了一下../repos/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/DoctrineAnnotations.php文件肯定被命中,并且需要使用正确的注释文件.

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.

由作曲家提供的版本:

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

任何帮助将不胜感激,因为我认为我不必在每个文件中添加use语句.

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

推荐答案

注释解析器的确要求在使用注释类之前将其导入.代替显式导入每个注释类,您可以执行以下操作(从

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

这篇关于如何正确地自动加载Doctrine ODM注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆