3

私はDoctrine 2を初めて使用し、注釈を使用してデータベースマッピングを行っています。もう少し進んで、いくつかのカスタム注釈を使用したいと思います。アノテーションで設定できるフォームなどを作れるようになることを目指しています。@Table などのクラスのアノテーションでさえ、パーサーから返されません。

Codeigniter 2 と Modular Extensions を使用しています。私のコントローラーには次のものがあります:

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

空の配列を返します。

次に、libraries/annotations フォルダー 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

事前にご協力いただきありがとうございます。

マーク。

4

2 に答える 2

3

使用する

Doctrine\Common\Annotations\AnnotationRegistry

AnnotationRegistry::RegisterLoader($universalClassLoader);
AnnotationRegistry::RegisterFile(__DIR__ . ' PATH_TO_DoctrineAnnotations.php ');
于 2012-11-19T04:19:34.793 に答える
0

お分かりのように、カスタム注釈ファイル/クラスを使用する前にそれらを含める必要があります。

それらをコントローラーに含めることはできますが、Doctrine の方法で行ってみませんか!

Doctrine2 の ORM には、フォルダー内に DoctrineAnnotations.php というファイルがありDoctrine/ORM/Mapping/Driver/ます。次のようになります。

...
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(すべて 1 行で):

resources.doctrine.orm.entityManagers.default
  .metadataDrivers.annotationRegistry.annotationFiles[]
  = APPLICATION_PATH "/path/to/my/library/ORM/Mapping/Driver/MyAnnotations.php"
于 2012-04-10T09:18:47.877 に答える