3

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();
}  
superdweebie
  • 1,576
  • 14
  • 23

1 Answers1

4

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 */
}
Ocramius
  • 25,171
  • 7
  • 103
  • 107
  • 1
    Thanks for the help. The docs on the doctrine site for the mongo compoent don't show the ODM\ namespace added to the annotations. I haven't yet used your solution, will have to wait a couple of days before I can get to the code again. Will post my results when I do. – superdweebie Apr 19 '12 at 11:03
  • You can still use the solution in the docs, but that is no more the suggested way, and is problematic as you would have conflicts with cases where you use ORM with the ODM (take @Id for example...). – Ocramius Apr 19 '12 at 13:00
  • 1
    Solution tested. Up and running. Thanks. – superdweebie Apr 22 '12 at 23:40