注释名称空间未加载针对Zend Framework 2的DoctrineMongoODMModule [英] Annotations Namespace not loaded DoctrineMongoODMModule for Zend Framework 2

查看:88
本文介绍了注释名称空间未加载针对Zend Framework 2的DoctrineMongoODMModule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经加载了zf2的Doctrine MongoODM模块。我的控制器内有文档管理器,一直进行得很好,直到我试图持有一个文档。它失败,出现此错误:



[语义错误]类SdsCore\Document\User中的注释@Document从未导入。



似乎在DocParser.php
的这一行失败if('\\'!== $ name [0]& &!$ this-> classExists($ name)){



它失败,因为 $ name ='文件',导入的注释类为'Doctrine\ODM\MongoDB\Mapping\Annotations\Doctrine'



这是我的文档类:

 命名空间SdsCore\Document; 

/ ** @Document * /
class User
{

/ **
* @Id(strategy =UUID )
* /
private $ id;

/ **
* @Field(type =string)
* /
private $ name;

/ **
* @Field(type =string)
* /
private $ firstname;

public function get($ property)
{
$ method ='get'.ucfirst($ property);
if(method_exists($ this,$ method))
{
return $ this-> $ method();
} else {
$ propertyName = $ property;
return $ this-> $ propertyName;
}
}

public function set($ property,$ value)
{
$ method ='set'.ucfirst($ property);
if(method_exists($ this,$ method))
{
$ this-> $ method($ value);
} else {
$ propertyName = $ property;
$ this-> $ propertyName = $ value;
}
}

}



这是我的动作控制器:

  public function indexAction()
{
$ dm = $ this-> documentManager;

$ user = new User();
$ user-> set('name','testname');
$ user-> set('firstname','testfirstname');
$ dm-> persist($ user);
$ dm-> flush;

返回新的ViewModel();
}


解决方案

我还没有工作在 DoctrineMongoODMModule 上,但下周我会来。无论如何,您仍然使用加载注释的旧方法。大多数教义项目现在都在使用 Doctrine\Common\Annotations\AnnotationReader ,而您的 @AnnotationName 告诉我您正在使用<一个href =https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Annotations/SimpleAnnotationReader.php =nofollow> Doctrine\Common\ Annotations\SimpeAnnotationReader 。您可以在 Doctrine\常见的文档



所以这里是如何修复你的文档:

 <?php 
命名空间SdsCore\Document;

使用Doctrine\ODM\MongoDB\Mapping\Annotations作为ODM;

/ ** @ ODM\Document * /
class User
{

/ **
* @ ODM\Id (strategy =UUID)
* /
private $ id;

/ **
* @ ODM\Field(type =string)
* /
private $ name;

/ **
* @ ODM\Field(type =string)
* /
private $ firstname;

/ * etc * /
}


I've loaded up the Doctrine MongoODM Module for zf2. I have got the document manager inside my controller, and all was going well until I tried to persist a document. It fails with this error:

"[Semantical Error] The annotation "@Document" in class SdsCore\Document\User was never imported."

It seems to fail on this line of DocParser.php if ('\\' !== $name[0] && !$this->classExists($name)) {

It fails because $name = 'Document', and the imported annotation class is 'Doctrine\ODM\MongoDB\Mapping\Annotations\Doctrine'

Here is my document class:

namespace SdsCore\Document;

/** @Document */
class User
{

/** 
 * @Id(strategy="UUID") 
 */
private $id;

/** 
 * @Field(type="string") 
 */
private $name;

/** 
 * @Field(type="string") 
 */
private $firstname;

public function get($property) 
{
    $method = 'get'.ucfirst($property);
    if (method_exists($this, $method))
    {
        return $this->$method();
    } else {
        $propertyName = $property;
        return $this->$propertyName;
    }           
}

public function set($property, $value) 
{
    $method = 'set'.ucfirst($property);
    if (method_exists($this, $method))
    {
        $this->$method($value);
    } else {
        $propertyName = $property;                
        $this->$propertyName = $value;
    }
}    

}

Here is my action controller:

public function indexAction()
{
    $dm = $this->documentManager;

    $user = new User();
    $user->set('name', 'testname');
    $user->set('firstname', 'testfirstname');
    $dm->persist($user);
    $dm->flush;

    return new ViewModel();
}  

解决方案

I didn't yet work on the DoctrineMongoODMModule, but I'll get to it next week. Anyway, you are still using the "old way" of loading annotations. Most of the doctrine projects are now using Doctrine\Common\Annotations\AnnotationReader, while your @AnnotationName tells me that you were using the Doctrine\Common\Annotations\SimpeAnnotationReader. You can read more about it at the Doctrine\Common documentation

So here's how to fix your document:

<?php
namespace SdsCore\Document;

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

/** @ODM\Document */
class User
{

    /** 
     * @ODM\Id(strategy="UUID") 
     */
    private $id;

    /** 
     * @ODM\Field(type="string") 
     */
    private $name;

    /** 
     * @ODM\Field(type="string") 
     */
    private $firstname;

    /* etc */
}

这篇关于注释名称空间未加载针对Zend Framework 2的DoctrineMongoODMModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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