11. Association Updates: Owning Side and Inverse Side¶
When mapping bidirectional associations it is important to understand the concept of the owning and inverse sides. The following general rules apply:
Relationships may be bidirectional or unidirectional.
関係は、双方向または単方向の場合があります。A bidirectional relationship has both an owning side and an inverse side
双方向の関係には、所有側と逆側の両方がありますA unidirectional relationship only has an owning side.
一方向の関係には、所有側しかありません。Doctrine will only check the owning side of an association for changes.
Doctrine は関連付けの所有側の変更のみをチェックします。
11.1. Bidirectional Associations¶
The following rules apply to bidirectional associations:
The inverse side has to have the
mappedBy
attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. ThemappedBy
attribute contains the name of the association-field on the owning side.逆側には、OneToOne、OneToMany、または ManyToMany マッピング宣言の mappingBy 属性が必要です。 mappingByattribute には、所有側の関連付けフィールドの名前が含まれています。The owning side has to have the
inversedBy
attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. TheinversedBy
attribute contains the name of the association-field on the inverse-side.所有側には、OneToOne、ManyToOne、または ManyToMany マッピング宣言の inversedBy 属性が必要です。inversedBy 属性には、逆側の関連付けフィールドの名前が含まれます。ManyToOne is always the owning side of a bidirectional association.
ManyToOne は、常に双方向の関連付けの所有側です。OneToMany is always the inverse side of a bidirectional association.
OneToMany は、常に双方向関連の逆側です。The owning side of a OneToOne association is the entity with the table containing the foreign key.
OneToOne アソシエーションの所有側は、外部キーを含むテーブルを持つエンティティです。You can pick the owning side of a many-to-many association yourself.
多対多関連付けの所有側を自分で選択できます。
11.2. Important concepts¶
Doctrine will only check the owning side of an association for changes.
To fully understand this, remember how bidirectional associations are maintained in the object world. There are 2 references on each side of the association and these 2 references both represent the same association but can change independently of one another. Of course, in a correct application the semantics of the bidirectional association are properly maintained by the application developer (that’s their responsibility). Doctrine needs to know which of these two in-memory references is the one that should be persisted and which not. This is what the owning/inverse concept is mainly used for.
Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view)
The owning side of a bidirectional association is the side Doctrine “looks at” when determining the state of the association, and consequently whether there is anything to do to update the association in the database.
Note
“Owning side” and “inverse side” are technical concepts of the ORM technology, not concepts of your domain model. What you consider as the owning side in your domain model can be different from what the owning side is for Doctrine. These are unrelated.