Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Doctrine 2 Annotation reader for customized annotations

I'm pretty new to Doctrine 2 and am using annotations to do my database mapping. I want to take things a little bit further and use some custom annotations. The aim is to be able to make forms and such that can have settings created through annotations. I'm having trouble reading ANY annotations - even class ones such as @Table aren't being returned from the parser.

I am using Codeigniter 2 and Modular Extensions. In my controller I have:

$reader = new \Doctrine\Common\Annotations\AnnotationReader();
$reader->setDefaultAnnotationNamespace('MyCompany\Annotations');
$reflClass = new ReflectionClass('models\User');
$classAnnotations = $reader->getClassAnnotations($reflClass);
print_r($classAnnotations);

Which returns an empty array.

I then have a file in my libraries/annotations folder, Bar.php:

namespace MyCompany\Annotations;

class Bar extends \Doctrine\Common\Annotations\Annotation
{
    public $foo;
}

and lastly, my user model:

/**
* @Entity
* @Table(name="user")
* @MyCompany\Annotations\Bar(foo="bar")
* @MyCompany\Annotations\Foo(bar="foo")
*/

class User {

}

I am trying to follow this example: http://www.doctrine-project.org/projects/common/2.0/docs/reference/annotations/en#setup-and-configuration

Thanks for your help in advance!

Mark.

like image 628
koosa Avatar asked Jul 27 '26 04:07

koosa


1 Answers

use

Doctrine\Common\Annotations\AnnotationRegistry

AnnotationRegistry::RegisterLoader($universalClassLoader);
AnnotationRegistry::RegisterFile(__DIR__ . ' PATH_TO_DoctrineAnnotations.php ');
like image 191
Faizal Pribadi Avatar answered Jul 28 '26 19:07

Faizal Pribadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!