1. Architecture

This chapter gives an overview of the overall architecture, terminology and constraints of Doctrine ORM. It is recommended to read this chapter carefully.

この章では、Doctrine ORM の全体的なアーキテクチャ、用語、および制約の概要を説明します。この章を注意深く読むことをお勧めします。

1.1. Using an Object-Relational Mapper

As the term ORM already hints at, Doctrine ORM aims to simplify the translation between database rows and the PHP object model. The primary use case for Doctrine are therefore applications that utilize the Object-Oriented Programming Paradigm. For applications that do not primarily work with objects Doctrine ORM is not suited very well.

ORM という用語がすでに示唆しているように、Doctrine ORM はデータベース行と PHP オブジェクト モデル間の変換を単純化することを目的としています。したがって、Doctrine の主な使用例は、オブジェクト指向プログラミング パラダイムを利用するアプリケーションです。主にオブジェクトを操作しないアプリケーションには、Doctrine ORM はあまり適していません。

1.2. Requirements

Doctrine ORM requires a minimum of PHP 7.1. For greatly improved performance it is also recommended that you use APC with PHP.

Doctrine ORM には最低でも PHP 7.1 が必要です。パフォーマンスを大幅に向上させるために、APC を PHP と共に使用することもお勧めします。

1.3. Doctrine ORM Packages

Doctrine ORM is divided into three main packages.

Doctrine ORM は 3 つの主要なパッケージに分かれています。

  • Common

    一般

  • DBAL (includes Common)

    DBAL (共通を含む)

  • ORM (includes DBAL+Common)

    ORM (DBAL+共通を含む)

This manual mainly covers the ORM package, sometimes touching parts of the underlying DBAL and Common packages. The Doctrine code base is split in to these packages for a few reasons and they are to…

このマニュアルは主に ORM パッケージを扱いますが、基礎となる DBAL および Common パッケージの一部に触れることもあります。 Doctrine のコードベースはいくつかの理由でこれらのパッケージに分割されており、それらは…

  • …make things more maintainable and decoupled

    …物事をより保守しやすく分離する

  • …allow you to use the code in Doctrine Common without the ORM or DBAL

    …ORMまたはDBALなしでDoctrine Commonのコードを使用できるようにする

  • …allow you to use the DBAL without the ORM

    …ORM なしで DBAL を使用できるようにする

1.3.1. The Common Package

The Common package contains highly reusable components that have no dependencies beyond the package itself (and PHP, of course). The root namespace of the Common package is Doctrine\Common.

Common パッケージには再利用性の高いコンポーネントが含まれており、パッケージ自体 (およびもちろん PHP) 以外に依存関係はありません。 Common パッケージのルート名前空間は Doctrine\Common です。

1.3.2. The DBAL Package

The DBAL package contains an enhanced database abstraction layer on top of PDO but is not strongly bound to PDO. The purpose of this layer is to provide a single API that bridges most of the differences between the different RDBMS vendors. The root namespace of the DBAL package is Doctrine\DBAL.

DBAL パッケージには、PDO の上に強化されたデータベース抽象化レイヤーが含まれていますが、PDO に強くバインドされていません。このレイヤーの目的は、異なる RDBMS ベンダー間のほとんどの違いを橋渡しする単一の API を提供することです。 DBAL パッケージのルート名前空間は Doctrine\DBAL です。

1.3.3. The ORM Package

The ORM package contains the object-relational mapping toolkit that provides transparent relational persistence for plain PHP objects. The root namespace of the ORM package is Doctrine\ORM.

ORM パッケージには、プレーンな PHP オブジェクトに透過的なリレーショナル永続性を提供するオブジェクト リレーショナル マッピング ツールキットが含まれています。ORM パッケージのルート名前空間は Doctrine\ORM です。

1.4. Terminology

1.4.1. Entities

An entity is a lightweight, persistent domain object. An entity can be any regular PHP class observing the following restrictions:

エンティティは、軽量で永続的なドメイン オブジェクトです。エンティティは、次の制限を遵守する任意の通常の PHP クラスにすることができます。

  • An entity class must not be final nor read-only but it may contain final methods or read-only properties.

    エンティティ クラスは final または読み取り専用であってはなりませんが、final メソッドまたは読み取り専用プロパティを含むことができます。

  • Any two entity classes in a class hierarchy that inherit directly or indirectly from one another must not have a mapped property with the same name. That is, if B inherits from A then B must not have a mapped field with the same name as an already mapped field that is inherited from A.

    相互に直接的または間接的に継承するクラス階層内の 2 つのエンティティ クラスは、同じ名前のマップされたプロパティを持ってはなりません。つまり、B が A から継承する場合、B には、A から継承された既にマップされたフィールドと同じ名前のマップされたフィールドがあってはなりません。

Entities support inheritance, polymorphic associations, and polymorphic queries. Both abstract and concrete classes can be entities. Entities may extend non-entity classes as well as entity classes, and non-entity classes may extend entity classes.

エンティティは、継承、ポリモーフィック アソシエーション、およびポリモーフィック クエリをサポートします。抽象クラスと具象クラスの両方をエンティティにすることができます。エンティティはエンティティ クラスと同様に非エンティティ クラスを拡張でき、非エンティティ クラスはエンティティ クラスを拡張できます。

Note

ノート

The constructor of an entity is only ever invoked when you construct a new instance with the new keyword. Doctrine never calls entity constructors, thus you are free to use them as you wish and even have it require arguments of any type.

エンティティのコンストラクターは、new キーワードを使用して新しいインスタンスを構築するときにのみ呼び出されます。 Doctrine は決してエンティティ コンストラクタを呼び出さないので、必要に応じて自由に使用でき、任意の型の引数を必要とすることさえできます。

1.4.2. Entity states

An entity instance can be characterized as being NEW, MANAGED, DETACHED or REMOVED.

エンティティ インスタンスは、NEW、MANAGED、DETACHED、または REMOVED として特徴付けることができます。

  • A NEW entity instance has no persistent identity, and is not yet associated with an EntityManager and a UnitOfWork (i.e. those just created with the “new” operator).

    NEW エンティティ インスタンスには永続的な ID がなく、EntityManager および UnitOfWork (つまり、「new」演算子で作成されたばかりのもの) にまだ関連付けられていません。

  • A MANAGED entity instance is an instance with a persistent identity that is associated with an EntityManager and whose persistence is thus managed.

    MANAGED エンティティ インスタンスは、EntityManager に関連付けられた永続アイデンティティを持つインスタンスであり、その永続性が管理されます。

  • A DETACHED entity instance is an instance with a persistent identity that is not (or no longer) associated with an EntityManager and a UnitOfWork.

    DETACHED エンティティ インスタンスは、EntityManager および UnitOfWork に関連付けられていない (または関連付けられていない) 永続的なアイデンティティを持つインスタンスです。

  • A REMOVED entity instance is an instance with a persistent identity, associated with an EntityManager, that will be removed from the database upon transaction commit.

    REMOVED エンティティ インスタンスは、トランザクション コミット時にデータベースから削除される、EntityManager に関連付けられた永続的な ID を持つインスタンスです。

1.4.3. Persistent fields

The persistent state of an entity is represented by instance variables. An instance variable must be directly accessed only from within the methods of the entity by the entity instance itself. Instance variables must not be accessed by clients of the entity. The state of the entity is available to clients only through the entity’s methods, i.e. accessor methods (getter/setter methods) or other business methods.

エンティティの永続的な状態は、インスタンス変数によって表されます。インスタンス変数は、エンティティ インスタンス自体によってエンティティのメソッド内からのみ直接アクセスする必要があります。インスタンス変数は、エンティティのクライアントからアクセスしてはなりません。エンティティの状態は、エンティティのメソッド、つまりアクセサ メソッド ( getter/setter メソッド) またはその他のビジネス メソッド。

Collection-valued persistent fields and properties must be defined in terms of the Doctrine\Common\Collections\Collection interface. The collection implementation type may be used by the application to initialize fields or properties before the entity is made persistent. Once the entity becomes managed (or detached), subsequent access must be through the interface type.

コレクション値の永続フィールドとプロパティは、Doctrine\Common\Collections\Collection インターフェイスで定義する必要があります。コレクション実装型は、エンティティが永続化される前に、フィールドまたはプロパティを初期化するためにアプリケーションによって使用される場合があります。エンティティが管理される (または分離される) と、その後のアクセスはインターフェイス タイプを介して行う必要があります。

1.4.4. Serializing entities

Serializing entities can be problematic and is not really recommended, at least not as long as an entity instance still holds references to proxy objects or is still managed by an EntityManager. By default, serializing proxy objects does not initialize them. On unserialization, resulting objects are detached from the entity manager and cannot be initialiazed anymore. You can implement the __serialize() method if you want to change that behavior, but then you need to ensure that you won’t generate large serialized object graphs and take care of circular associations.

エンティティのシリアル化は問題を引き起こす可能性があり、少なくともエンティティ インスタンスがプロキシ オブジェクトへの参照を保持している、または EntityManager によって管理されている限り、実際には推奨されません。デフォルトでは、プロキシ オブジェクトをシリアル化してもそれらは初期化されません。シリアル化を解除すると、結果のオブジェクトはエンティティ マネージャーから切り離され、初期化できなくなります。その動作を変更したい場合は __serialize() メソッドを実装できますが、その場合、大きなシリアライズされたオブジェクト グラフを生成しないようにし、循環的な関連付けに注意する必要があります。

1.4.5. The EntityManager

The EntityManager class is a central access point to the functionality provided by Doctrine ORM. The EntityManager API is used to manage the persistence of your objects and to query for persistent objects.

EntityManager クラスは、Doctrine ORM によって提供される機能への中央アクセス ポイントです。 EntityManager API は、オブジェクトの永続性を管理し、永続オブジェクトをクエリするために使用されます。

1.4.6. Transactional write-behind

An EntityManager and the underlying UnitOfWork employ a strategy called “transactional write-behind” that delays the execution of SQL statements in order to execute them in the most efficient way and to execute them at the end of a transaction so that all write locks are quickly released. You should see Doctrine as a tool to synchronize your in-memory objects with the database in well defined units of work. Work with your objects and modify them as usual and when you’re done call EntityManager#flush() to make your changes persistent.

EntityManager と基礎となる UnitOfWork は、SQL ステートメントの実行を遅らせて最も効率的な方法で実行し、すべての書き込みロックが迅速に解放されるようにトランザクションの最後に実行する「トランザクション ライト ビハインド」と呼ばれる戦略を採用しています。 Doctrine は、明確に定義された作業単位でメモリ内オブジェクトをデータベースと同期するためのツールであることがわかるはずです。オブジェクトを操作して通常どおりに変更し、完了したら EntityManager#flush() を呼び出して変更を永続化します。

1.4.7. The Unit of Work

Internally an EntityManager uses a UnitOfWork, which is a typical implementation of the Unit of Work pattern, to keep track of all the things that need to be done the next time flush is invoked. You usually do not directly interact with a UnitOfWork but with the EntityManager instead.

内部的には、EntityManager は、Unit of Work パターンの典型的な実装である UnitOfWork を使用して、次にフラッシュが呼び出されたときに実行する必要があるすべてのことを追跡します。通常、aUnitOfWork と直接対話するのではなく、代わりに EntityManager と対話します。

Table Of Contents

Previous topic

Separating Concerns using Embeddables

Embeddables を使用して懸念事項を分離する

Next topic

2. Installation and Configuration

2. インストールと設定

This Page

Fork me on GitHub