23. PHP Mapping

Doctrine ORM also allows you to provide the ORM metadata in the form of plain PHP code using the ClassMetadata API. You can write the code in PHP files or inside of a static function named loadMetadata($class) on the entity class itself.

Doctrine ORM では、ClassMetadata API を使用してプレーンな PHP コードの形式で ORM メタデータを提供することもできます。コードは、PHP ファイル内、またはエンティティ クラス自体の loadMetadata($class) という名前の静的関数内に記述できます。

23.1. PHP Files

Note

ノート

PHPDriver is deprecated and will be removed in 3.0, use StaticPHPDriver instead.

PHPDriver は非推奨であり、3.0 で削除される予定です。代わりに StaticPHPDriver を使用してください。

If you wish to write your mapping information inside PHP files that are named after the entity and included to populate the metadata for an entity you can do so by using the PHPDriver:

エンティティにちなんで名付けられ、エンティティのメタデータを入力するために含まれる PHP ファイル内にマッピング情報を書き込みたい場合は、PHPDriver を使用して行うことができます。

<?php
$driver = new PHPDriver('/path/to/php/mapping/files');
$em->getConfiguration()->setMetadataDriverImpl($driver);

Now imagine we had an entity named Entities\User and we wanted to write a mapping file for it using the above configured PHPDriver instance:

ここで、Entities\User という名前のエンティティがあり、上記で構成した PHPDriver インスタンスを使用してマッピング ファイルを作成したいとします。

<?php
namespace Entities;

class User
{
    private $id;
    private $username;
}

To write the mapping information you just need to create a file named Entities.User.php inside of the /path/to/php/mapping/files folder:

マッピング情報を書き込むには、/path/to/php/mapping/files フォルダー内にファイル名の Entities.User.php を作成する必要があります。

<?php
// /path/to/php/mapping/files/Entities.User.php

$metadata->mapField(array(
   'id' => true,
   'fieldName' => 'id',
   'type' => 'integer'
));

$metadata->mapField(array(
   'fieldName' => 'username',
   'type' => 'string',
   'options' => array(
       'fixed' => true,
       'comment' => "User's login name"
   )
));

$metadata->mapField(array(
   'fieldName' => 'login_count',
   'type' => 'integer',
   'nullable' => false,
   'options' => array(
       'unsigned' => true,
       'default' => 0
   )
));

Now we can easily retrieve the populated ClassMetadata instance where the PHPDriver includes the file and the ClassMetadataFactory caches it for later retrieval:

これで、PHPDriver がファイルをインクルードし、後で取得するために ClassMetadataFactory がそれをキャッシュする、移入された ClassMetadata インスタンスを簡単に取得できます。

<?php
$class = $em->getClassMetadata('Entities\User');
// or
$class = $em->getMetadataFactory()->getMetadataFor('Entities\User');

23.2. Static Function

In addition to the PHP files you can also specify your mapping information inside of a static function defined on the entity class itself. This is useful for cases where you want to keep your entity and mapping information together but don’t want to use attributes or annotations. For this you just need to use the StaticPHPDriver:

PHP ファイルに加えて、エンティティ クラス自体で定義された静的関数内でマッピング情報を指定することもできます。これは、エンティティとマッピング情報を一緒に保持したいが、属性や注釈を使用したくない場合に役立ちます。これには、StaticPHPDriver を使用する必要があります。

<?php
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;

$driver = new StaticPHPDriver('/path/to/entities');
$em->getConfiguration()->setMetadataDriverImpl($driver);

Now you just need to define a static function named loadMetadata($metadata) on your entity:

次に、エンティティで loadMetadata($metadata) という名前の静的関数を定義する必要があります。

<?php
namespace Entities;

use Doctrine\ORM\Mapping\ClassMetadata;

class User
{
    // ...

    public static function loadMetadata(ClassMetadata $metadata)
    {
        $metadata->mapField(array(
           'id' => true,
           'fieldName' => 'id',
           'type' => 'integer'
        ));

        $metadata->mapField(array(
           'fieldName' => 'username',
           'type' => 'string'
        ));
    }
}

23.3. ClassMetadataBuilder

To ease the use of the ClassMetadata API (which is very raw) there is a ClassMetadataBuilder that you can use.

ClassMetadata API (非常に未加工) の使用を容易にするために、使用できる ClassMetadataBuilder があります。

<?php
namespace Entities;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;

class User
{
    // ...

    public static function loadMetadata(ClassMetadata $metadata)
    {
        $builder = new ClassMetadataBuilder($metadata);
        $builder->createField('id', 'integer')->isPrimaryKey()->generatedValue()->build();
        $builder->addField('username', 'string');
    }
}

The API of the ClassMetadataBuilder has the following methods with a fluent interface:

ClassMetadataBuilder の API には、流暢なインターフェースを備えた次のメソッドがあります。

  • addField($name, $type, array $mapping)

    addField($name, $type, array $mapping)

  • setMappedSuperclass()

    setMappedSuperclass()

  • setReadOnly()

    setReadOnly()

  • setCustomRepositoryClass($className)

    setCustomRepositoryClass($className)

  • setTable($name)

    setTable($name)

  • addIndex(array $columns, $indexName)

    addIndex(配列 $columns, $indexName)

  • addUniqueConstraint(array $columns, $constraintName)

    addUniqueConstraint(配列 $columns, $constraintName)

  • addNamedQuery($name, $dqlQuery)

    addNamedQuery($name, $dqlQuery)

  • setJoinedTableInheritance()

    setJoinedTableInheritance()

  • setSingleTableInheritance()

    setSingleTableInheritance()

  • setDiscriminatorColumn($name, $type = 'string', $length = 255, $columnDefinition = null, $enumType = null)

    setDiscriminatorColumn($name, $type = 'string', $length = 255, $columnDefinition = null, $enumType = null)

  • addDiscriminatorMapClass($name, $class)

    addDiscriminatorMapClass($name, $class)

  • setChangeTrackingPolicyDeferredExplicit()

    setChangeTrackingPolicyDeferredExplicit()

  • setChangeTrackingPolicyNotify()

    setChangeTrackingPolicyNotify()

  • addLifecycleEvent($methodName, $event)

    addLifecycleEvent($メソッド名, $イベント)

  • addManyToOne($name, $targetEntity, $inversedBy = null)

    addManyToOne($name, $targetEntity, $inversedBy = null)

  • addInverseOneToOne($name, $targetEntity, $mappedBy)

    addInverseOneToOne($name, $targetEntity, $mappedBy)

  • addOwningOneToOne($name, $targetEntity, $inversedBy = null)

    addOwningOneToOne($name, $targetEntity, $inversedBy = null)

  • addOwningManyToMany($name, $targetEntity, $inversedBy = null)

    addOwningManyToMany($name, $targetEntity, $inversedBy = null)

  • addInverseManyToMany($name, $targetEntity, $mappedBy)

    addInverseManyToMany($name, $targetEntity, $mappedBy)

  • addOneToMany($name, $targetEntity, $mappedBy)

    addOneToMany($name, $targetEntity, $mappedBy)

It also has several methods that create builders (which are necessary for advanced mappings):

また、ビルダーを作成するメソッドもいくつかあります (高度なマッピングに必要です)。

  • createField($name, $type) returns a FieldBuilder instance

    createField($name, $type) は FieldBuilder インスタンスを返します

  • createManyToOne($name, $targetEntity) returns an AssociationBuilder instance

    createManyToOne($name, $targetEntity) は AssociationBuilder インスタンスを返します

  • createOneToOne($name, $targetEntity) returns an AssociationBuilder instance

    createOneToOne($name, $targetEntity) は AssociationBuilder インスタンスを返します

  • createManyToMany($name, $targetEntity) returns an ManyToManyAssociationBuilder instance

    createManyToMany($name, $targetEntity) は ManyToManyAssociationBuilder インスタンスを返します

  • createOneToMany($name, $targetEntity) returns an OneToManyAssociationBuilder instance

    createOneToMany($name, $targetEntity) は OneToManyAssociationBuilder インスタンスを返します

23.4. ClassMetadata API

The ClassMetadata class is the data object for storing the mapping metadata for a single entity. It contains all the getters and setters you need populate and retrieve information for an entity.

ClassMetadata クラスは、単一エンティティのマッピング メタデータを格納するためのデータ オブジェクトです。これには、エンティティの情報を入力および取得する必要があるすべてのゲッターとセッターが含まれています。

23.4.1. General Setters

  • setTableName($tableName)

    setTableName($テーブル名)

  • setPrimaryTable(array $primaryTableDefinition)

    setPrimaryTable(配列 $primaryTableDefinition)

  • setCustomRepositoryClass($repositoryClassName)

    setCustomRepositoryClass($repositoryClassName)

  • setIdGeneratorType($generatorType)

    setIdGeneratorType($generatorType)

  • setIdGenerator($generator)

    setIdGenerator($generator)

  • setSequenceGeneratorDefinition(array $definition)

    setSequenceGeneratorDefinition(配列 $definition)

  • setChangeTrackingPolicy($policy)

    setChangeTrackingPolicy($policy)

  • setIdentifier(array $identifier)

    setIdentifier(配列 $identifier)

23.4.2. Inheritance Setters

  • setInheritanceType($type)

    setInheritanceType($type)

  • setSubclasses(array $subclasses)

    setSubclasses(配列 $subclasses)

  • setParentClasses(array $classNames)

    setParentClasses(配列 $classNames)

  • setDiscriminatorColumn($columnDef)

    setDiscriminatorColumn($columnDef)

  • setDiscriminatorMap(array $map)

    setDiscriminatorMap(配列 $map)

23.4.3. Field Mapping Setters

  • mapField(array $mapping)

    mapField(配列 $mapping)

  • mapOneToOne(array $mapping)

    mapOneToOne(配列 $mapping)

  • mapOneToMany(array $mapping)

    mapOneToMany(配列 $mapping)

  • mapManyToOne(array $mapping)

    mapManyToOne(配列 $mapping)

  • mapManyToMany(array $mapping)

    mapManyToMany(配列 $mapping)

23.4.4. Lifecycle Callback Setters

  • addLifecycleCallback($callback, $event)

    addLifecycleCallback($callback, $event)

  • setLifecycleCallbacks(array $callbacks)

    setLifecycleCallbacks(array $callbacks)

23.4.5. Versioning Setters

  • setVersionMapping(array &$mapping)

  • setVersioned($bool)

    setVersioned($bool)

  • setVersionField()

    setVersionField()

23.4.6. General Getters

  • getTableName()

    getTableName()

  • getSchemaName()

    getSchemaName()

  • getTemporaryIdTableName()

    getTemporaryIdTableName()

23.4.7. Identifier Getters

  • getIdentifierColumnNames()

    getIdentifierColumnNames()

  • usesIdGenerator()

    usesIdGenerator()

  • isIdentifier($fieldName)

    isIdentifier($フィールド名)

  • isIdGeneratorIdentity()

    isIdGeneratorIdentity()

  • isIdGeneratorSequence()

    isIdGeneratorSequence()

  • isIdGeneratorTable()

    isIdGeneratorTable()

  • isIdentifierNatural()

    isIdentifierNatural()

  • getIdentifierFieldNames()

    getIdentifierFieldNames()

  • getSingleIdentifierFieldName()

    getSingleIdentifierFieldName()

  • getSingleIdentifierColumnName()

    getSingleIdentifierColumnName()

23.4.8. Inheritance Getters

  • isInheritanceTypeNone()

    isInheritanceTypeNone()

  • isInheritanceTypeJoined()

    isInheritanceTypeJoined()

  • isInheritanceTypeSingleTable()

    isInheritanceTypeSingleTable()

  • isInheritanceTypeTablePerClass()

    isInheritanceTypeTablePerClass()

  • isInheritedField($fieldName)

    isInheritedField($フィールド名)

  • isInheritedAssociation($fieldName)

    isInheritedAssociation($フィールド名)

23.4.9. Change Tracking Getters

  • isChangeTrackingDeferredExplicit()

    isChangeTrackingDeferredExplicit()

  • isChangeTrackingDeferredImplicit()

    isChangeTrackingDeferredImplicit()

  • isChangeTrackingNotify()

    isChangeTrackingNotify()

23.4.10. Field & Association Getters

  • isUniqueField($fieldName)

    isUniqueField($フィールド名)

  • isNullable($fieldName)

    isNullable($フィールド名)

  • getColumnName($fieldName)

    getColumnName($フィールド名)

  • getFieldMapping($fieldName)

    getFieldMapping($フィールド名)

  • getAssociationMapping($fieldName)

    getAssociationMapping($fieldName)

  • getAssociationMappings()

    getAssociationMappings()

  • getFieldName($columnName)

    getFieldName($列名)

  • hasField($fieldName)

    hasField($フィールド名)

  • getColumnNames(array $fieldNames = null)

    getColumnNames(array $fieldNames = null)

  • getTypeOfField($fieldName)

    getTypeOfField($フィールド名)

  • getTypeOfColumn($columnName)

    getTypeOfColumn($列名)

  • hasAssociation($fieldName)

    hasAssociation($フィールド名)

  • isSingleValuedAssociation($fieldName)

    isSingleValuedAssociation($fieldName)

  • isCollectionValuedAssociation($fieldName)

    isCollectionValuedAssociation($fieldName)

23.4.11. Lifecycle Callback Getters

  • hasLifecycleCallbacks($lifecycleEvent)

    hasLifecycleCallbacks($lifecycleEvent)

  • getLifecycleCallbacks($event)

    getLifecycleCallbacks($event)

23.4.12. Runtime reflection methods

These are methods related to runtime reflection for working with the entities themselves.

これらは、エンティティ自体を操作するためのランタイム リフレクションに関連するメソッドです。

  • getReflectionClass()

    getReflectionClass()

  • getReflectionProperties()

    getReflectionProperties()

  • getReflectionProperty($name)

    getReflectionProperty($name)

  • getSingleIdReflectionProperty()

    getSingleIdReflectionProperty()

  • getIdentifierValues($entity)

    getIdentifierValues($エンティティ)

  • setIdentifierValues($entity, $id)

    setIdentifierValues($エンティティ、$id)

  • setFieldValue($entity, $field, $value)

    setFieldValue($エンティティ、$フィールド、$値)

  • getFieldValue($entity, $field)

    getFieldValue($エンティティ、$フィールド)

Table Of Contents

Previous topic

22. YAML Mapping

22. YAML マッピング

Next topic

24. Caching

24. キャッシング

This Page

Fork me on GitHub