22. YAML Mapping

Warning

警告

The YAML driver is deprecated and will be removed in version 3.0. It is strongly recommended to switch to one of the other mappings.

YAML ドライバーは推奨されておらず、バージョン 3.0 で削除されます。他のマッピングのいずれかに切り替えることを強くお勧めします。

The YAML mapping driver enables you to provide the ORM metadata in form of YAML documents.

YAML マッピング ドライバーを使用すると、YAML ドキュメントの ORM メタデータ情報を提供できます。

The YAML mapping document of a class is loaded on-demand the first time it is requested and subsequently stored in the metadata cache. In order to work, this requires certain conventions:

クラスの YAML マッピング ドキュメントは、最初に要求されたときにオンデマンドで読み込まれ、その後メタデータ キャッシュに保存されます。これを機能させるには、次の規則が必要です。

  • Each entity/mapped superclass must get its own dedicated YAML mapping document.

    各エンティティ/マップされたスーパークラスは、専用の YAML マッピング ドキュメントを取得する必要があります。

  • The name of the mapping document must consist of the fully qualified name of the class, where namespace separators are replaced by dots (.).

    マッピング ドキュメントの名前は、クラスの完全修飾名で構成する必要があります。名前空間の区切り文字はドット (.) に置き換えられます。

  • All mapping documents should get the extension “.dcm.yml” to identify it as a Doctrine mapping file. This is more of a convention and you are not forced to do this. You can change the file extension easily enough.

    Doctrine マッピング ファイルであることを識別するために、すべてのマッピング ドキュメントに拡張子「.dcm.yml」を付ける必要があります。これは慣習的なものであり、これを強制されるものではありません。ファイル拡張子は簡単に変更できます。

<?php
$driver->setFileExtension('.yml');

It is recommended to put all YAML mapping documents in a single folder but you can spread the documents over several folders if you want to. In order to tell the YamlDriver where to look for your mapping documents, supply an array of paths as the first argument of the constructor, like this:

すべての YAML マッピング ドキュメントを 1 つのフォルダーに配置することをお勧めしますが、必要に応じて複数のフォルダーにドキュメントを分散させることもできます。マッピング ドキュメントを検索する場所を YamlDriver に指示するには、次のように、コンストラクターの最初の引数としてパスの配列を指定します。

<?php
use Doctrine\ORM\Mapping\Driver\YamlDriver;

// $config instanceof Doctrine\ORM\Configuration
$driver = new YamlDriver(array('/path/to/files'));
$config->setMetadataDriverImpl($driver);

22.1. Simplified YAML Driver

The Symfony project sponsored a driver that simplifies usage of the YAML Driver. The changes between the original driver are:

Symfony プロジェクトは、YAML ドライバーの使用を簡素化するドライバーを後援しました。元のドライバーとの変更点は次のとおりです。

  • File Extension is .orm.yml

    ファイル拡張子は .orm.yml

  • Filenames are shortened, “MyProject\Entities\User” will become User.orm.yml

    ファイル名は短縮され、「MyProject\Entities\User」は User.orm.yml になります。

  • You can add a global file and add multiple entities in this file.

    グローバル ファイルを追加し、このファイルに複数のエンティティを追加できます。

Configuration of this client works a little bit different:

このクライアントの構成は少し異なります。

<?php
$namespaces = array(
    '/path/to/files1' => 'MyProject\Entities',
    '/path/to/files2' => 'OtherProject\Entities'
);
$driver = new \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver($namespaces);
$driver->setGlobalBasename('global'); // global.orm.yml

22.1.1. Example

As a quick start, here is a small example document that makes use of several common elements:

クイック スタートとして、いくつかの一般的な要素を使用する小さなサンプル ドキュメントを次に示します。

# Doctrine.Tests.ORM.Mapping.User.dcm.yml
Doctrine\Tests\ORM\Mapping\User:
  type: entity
  repositoryClass: Doctrine\Tests\ORM\Mapping\UserRepository
  table: cms_users
  schema: schema_name # The schema the table lies in, for platforms that support schemas (Optional, >= 2.5)
  readOnly: true
  indexes:
    name_index:
      columns: [ name ]
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 50
    email:
      type: string
      length: 32
      column: user_email
      unique: true
      options:
        fixed: true
        comment: User's email address
    loginCount:
      type: integer
      column: login_count
      nullable: false
      options:
        unsigned: true
        default: 0
  oneToOne:
    address:
      targetEntity: Address
      joinColumn:
        name: address_id
        referencedColumnName: id
        onDelete: CASCADE
  oneToMany:
    phonenumbers:
      targetEntity: Phonenumber
      mappedBy: user
      cascade: ["persist", "merge"]
  manyToMany:
    groups:
      targetEntity: Group
      joinTable:
        name: cms_users_groups
        joinColumns:
          user_id:
            referencedColumnName: id
        inverseJoinColumns:
          group_id:
            referencedColumnName: id
  lifecycleCallbacks:
    prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ]
    postPersist: [ doStuffOnPostPersist ]

Be aware that class-names specified in the YAML files should be fully qualified.

YAML ファイルで指定されたクラス名は完全に修飾する必要があることに注意してください。

22.2. Reference

22.2.1. Unique Constraints

It is possible to define unique constraints by the following declaration:

次の宣言によって一意の制約を定義することができます。

# ECommerceProduct.orm.yml
ECommerceProduct:
  type: entity
  fields:
    # definition of some fields
  uniqueConstraints:
    search_idx:
      columns: [ name, email ]

Table Of Contents

Previous topic

21. XML Mapping

21. XML マッピング

Next topic

23. PHP Mapping

23. PHP マッピング

This Page

Fork me on GitHub