2012-04-19 74 views
3

我已經加載了用於zf2的Doctrine MongoODM模塊。我在我的控制器裏面有文檔管理器,並且在我試圖保留一個文檔之前一切都很順利。它失敗並出現此錯誤:Annotations命名空間未加載Zend Framework 2的DoctrineMongoODMModule

「[語義錯誤]類SdsCore \ Document \ User中的註釋」@Document「從未導入。」

它似乎無法在這條線DocParser.php 的if ('\\' !== $name[0] && !$this->classExists($name)) {

它失敗,因爲$name = 'Document',並且導入的註釋類爲'Doctrine\ODM\MongoDB\Mapping\Annotations\Doctrine'

這裏是我的文檔類:

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

}

這是我的動作控制器:

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

回答

4

我還沒有在DoctrineMongoODMModule上工作,但我會在下週進行討論。無論如何,你仍然在使用加載註釋的「舊方法」。大多數學說項目現在使用Doctrine\Common\Annotations\AnnotationReader,而你的@AnnotationName告訴我你正在使用Doctrine\Common\Annotations\SimpeAnnotationReader。您可以在Doctrine\Common documentation

閱讀更多關於它因此,這裏是如何解決您的文檔:

<?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 */ 
} 
+1

感謝您的幫助。 mongo compoent的doctrine網站上的文檔沒有顯示添加到註釋中的ODM \ namespace。我還沒有使用你的解決方案,必須等待幾天才能再次訪問代碼。我會做我的結果。 – superdweebie 2012-04-19 11:03:22

+0

您仍然可以在文檔中使用該解決方案,但這不再是建議的方式,並且存在問題,因爲您將ODM與ODM一起使用(例如,採用@Id ...)的情況會發生衝突。 – Ocramius 2012-04-19 13:00:34

+1

解決方案經過測試。正在運行。謝謝。 – superdweebie 2012-04-22 23:40:04

相關問題