我已經加載了用於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();
}
感謝您的幫助。 mongo compoent的doctrine網站上的文檔沒有顯示添加到註釋中的ODM \ namespace。我還沒有使用你的解決方案,必須等待幾天才能再次訪問代碼。我會做我的結果。 – superdweebie 2012-04-19 11:03:22
您仍然可以在文檔中使用該解決方案,但這不再是建議的方式,並且存在問題,因爲您將ODM與ODM一起使用(例如,採用@Id ...)的情況會發生衝突。 – Ocramius 2012-04-19 13:00:34
解決方案經過測試。正在運行。謝謝。 – superdweebie 2012-04-22 23:40:04