使用 Doctrine 2 Annotation reader 进行自定义注释

发布于 2024-11-25 10:45:31 字数 1136 浏览 4 评论 0原文

我对 Doctrine 2 还很陌生,并且正在使用注释来进行数据库映射。我想更进一步并使用一些自定义注释。目的是能够制作可以通过注释创建设置的表单等。我在读取任何注释时都遇到问题 - 即使类注释(例如@Table)也不会从解析器返回。

我正在使用 Codeigniter 2 和模块化扩展。在我的控制器中,我有:

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

返回一个空数组。

然后,我的库/注释文件夹中有一个文件 Bar.php:

namespace MyCompany\Annotations;

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

最后是我的用户模型:

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

class User {

}

我正在尝试遵循以下示例: http://www.doctrine -project.org/projects/common/2.0/docs/reference/annotations/en#setup-and-configuration

提前感谢您的帮助!

标记。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小镇女孩 2024-12-02 10:45:31

使用

Doctrine\Common\Annotations\AnnotationRegistry

AnnotationRegistry::RegisterLoader($universalClassLoader);
AnnotationRegistry::RegisterFile(__DIR__ . ' PATH_TO_DoctrineAnnotations.php ');

use

Doctrine\Common\Annotations\AnnotationRegistry

AnnotationRegistry::RegisterLoader($universalClassLoader);
AnnotationRegistry::RegisterFile(__DIR__ . ' PATH_TO_DoctrineAnnotations.php ');
深陷 2024-12-02 10:45:31

正如您所发现的,您需要先包含自定义注释文件/类,然后才能使用它们。

虽然将它们包含在你的控制器中是可行的,但为什么不按照 Doctrine 的方式来做呢!

Doctrine2 的 ORM 在 Doctrine/ORM/Mapping/Driver/ 文件夹中有一个名为 DoctrineAnnotations.php 的文件。它看起来像这样:

...
require_once __DIR__.'/../GeneratedValue.php';
require_once __DIR__.'/../Version.php';
require_once __DIR__.'/../JoinColumn.php';
require_once __DIR__.'/../JoinColumns.php';
require_once __DIR__.'/../Column.php';
...

所以,我所做的就是在我的库中创建一个类似的文件,并通过包含这个“驱动程序”(例如在您的引导程序中)来加载我的注释。

在我基于 ZF 的应用程序中(使用 Guilherme Blanco 出色的 Zf1-D2 设置),我只是将我的“注释驱动程序”添加到我的 application.ini 中,如下所示(全部在一行中):

resources.doctrine.orm.entityManagers.default
  .metadataDrivers.annotationRegistry.annotationFiles[]
  = APPLICATION_PATH "/path/to/my/library/ORM/Mapping/Driver/MyAnnotations.php"

As you've figured out, you need to include your custom annotation files/classes before you can use them.

Though including them in your controller would work, why not do it the Doctrine way!

Doctrine2's ORM has a file called DoctrineAnnotations.php in the Doctrine/ORM/Mapping/Driver/ folder. It looks like this:

...
require_once __DIR__.'/../GeneratedValue.php';
require_once __DIR__.'/../Version.php';
require_once __DIR__.'/../JoinColumn.php';
require_once __DIR__.'/../JoinColumns.php';
require_once __DIR__.'/../Column.php';
...

So, what I've done is created a similar file in my library and load my annotations by including this "driver" (eg. in your bootstrap).

In my ZF-based app (using Guilherme Blanco's fabulous Zf1-D2 set-up), I just added my "annotations driver" to my application.ini, like this (all on one line):

resources.doctrine.orm.entityManagers.default
  .metadataDrivers.annotationRegistry.annotationFiles[]
  = APPLICATION_PATH "/path/to/my/library/ORM/Mapping/Driver/MyAnnotations.php"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文