Extra Lazy Associations

New in version 2.1.

バージョン 2.1 の新機能。

In many cases associations between entities can get pretty large. Even in a simple scenario like a blog. where posts can be commented, you always have to assume that a post draws hundreds of comments. In Doctrine ORM if you accessed an association it would always get loaded completely into memory. This can lead to pretty serious performance problems, if your associations contain several hundreds or thousands of entities.

多くの場合、エンティティ間の関連付けはかなり大きくなります。投稿にコメントできるブログのような単純なシナリオであっても、投稿には何百ものコメントが寄せられると常に想定する必要があります。Doctrine ORM では、アソシエーションにアクセスすると、常に完全にメモリにロードされます。これは、アソシエーションに数百または数千のエンティティが含まれている場合、かなり深刻なパフォーマンスの問題を引き起こす可能性があります。

Doctrine ORM includes a feature called Extra Lazy for associations. Associations are marked as Lazy by default, which means the whole collection object for an association is populated the first time its accessed. If you mark an association as extra lazy the following methods on collections can be called without triggering a full load of the collection:

Doctrine ORM には、関連付け用の Extra Lazy と呼ばれる機能が含まれています。アソシエーションはデフォルトで Lazy としてマークされます。これは、アソシエーションのコレクション オブジェクト全体が最初にアクセスされたときに設定されることを意味します。アソシエーションをエクストラ レイジーとしてマークすると、コレクションの全ロードをトリガーすることなく、コレクションの次のメソッドを呼び出すことができます。

  • Collection#contains($entity)

    Collection#contains($entity)

  • Collection#containsKey($key)

    Collection#containsKey($key)

  • Collection#count()

    コレクション#count()

  • Collection#get($key)

    Collection#get($key)

  • Collection#slice($offset, $length = null)

    Collection#slice($offset, $length = null)

For each of the above methods the following semantics apply:

上記の各メソッドには、次のセマンティクスが適用されます。

  • For each call, if the Collection is not yet loaded, issue a straight SELECT statement against the database.

    呼び出しごとに、コレクションがまだロードされていない場合は、データベースに対して単純な SELECT ステートメントを発行します。

  • For each call, if the collection is already loaded, fallback to the default functionality for lazy collections. No additional SELECT statements are executed.

    呼び出しごとに、コレクションが既に読み込まれている場合は、遅延コレクションの既定の機能にフォールバックします。追加の SELECT ステートメントは実行されません。

Additionally even with Doctrine ORM the following methods do not trigger the collection load:

さらに、Doctrine ORM を使用しても、次のメソッドはコレクションのロードをトリガーしません:

  • Collection#add($entity)

    Collection#add($entity)

  • Collection#offsetSet($key, $entity) - ArrayAccess with no specific key $coll[] = $entity, it does not work when setting specific keys like $coll[0] = $entity.

    Collection#offsetSet($key, $entity) - 特定のキー $coll[] = $entity のない ArrayAccess。$coll[0] = $entity のような特定のキーを設定すると機能しません。

With extra lazy collections you can now not only add entities to large collections but also paginate them easily using a combination of count and slice.

余分な遅延コレクションを使用すると、大規模なコレクションにエンティティを追加できるだけでなく、カウントとスライスの組み合わせを使用して簡単にページ付けすることもできます。

Warning

警告

removeElement directly issued DELETE queries to the database from version 2.4.0 to 2.7.0. This circumvents the flush operation and might run outside a transactional boundary if you don’t create one yourself. We consider this a critical bug in the assumption of how the ORM works and reverted removeElement EXTRA_LAZY behavior in 2.7.1.

removeElement は、バージョン 2.4.0 から 2.7.0 までのデータベースに対して DELETE クエリを直接発行しました。 ORM がどのように機能するかを前提として、これは重大なバグであると考えて、2.7.1 で removeElement EXTRA_LAZY の動作を元に戻しました。

Enabling Extra-Lazy Associations

The mapping configuration is simple. Instead of using the default value of fetch="LAZY" you have to switch to extra lazy as shown in these examples:

マッピング構成は単純です。デフォルト値の fetch="LAZY" を使用する代わりに、次の例に示すように、extra lazy に切り替える必要があります。

Table Of Contents

Previous topic

Working with Indexed Associations

索引付けされた関連付けの操作

Next topic

Composite and Foreign Keys as Primary Key

主キーとしての複合キーと外部キー

This Page

Fork me on GitHub