Doctrine Entity 中 什么时候用inversedBy和mappedBy?

(1) mappedBy 必须被指定在相反方;

(2) inversedBy 必须被指定在拥有方;

(3) OneToOne


拥有方:是一个包含外键的表的实体,即建外键的表的那一方。


如a表中id跟b、c、d中的a_id一对一关联,

a # OneToOne
        
id name
1   a_1
2   a_2
3   a_3
4   a_4
        
b # OneToOne
        
a_id name
2    b
        
c # OneToOne
        
a_id name
3    c
        
d # OneToOne
        
a_id name
4    d

为了保证数据的一致性,我们一般会在b、c、d中分别增加一个跟a表的id关联的外键,故在此例中b、c、d为拥有方。


我们也可以这样理解:


a中有的数据b中不一定有(如3、4),b中有的a_id数据a中一定有;

a中有的数据c中不一定有(如2、4),c中有的a_id数据a中一定有;

a中有的数据c中不一定有(如2、3),d中有的a_id数据a中一定有;

那么我们必须是从b、c、d出发才一定会找到数据,不会报错,那么他们就是拥有方。


(4) ManyToOne 和 OneToMany


ManyToOne : 拥有方

OneToMany : 相反方


如a表跟b表 一对多关系

a  # OneToMany
        
id   name
1    a_1
2    a_2
        
b # ManyToOne
        
id a_id name
1   1    b_1
2   1    b_2
3   1    b_3

为了保证数据一致性,我们会在b表中建立一个跟a表id关联的外键,那么b就是拥有方

(5) @ORMJoinColumn(name="icon", referencedColumnName="id") 仅写在拥有方。

(6) 如不了解外键请参考:MYSQL外键(Foreign Key)的使用

(7) @ORMJoinColumn的name、referencedColumnName也可以不指定,默认为“主键”字段

(8) Doctrine只会检查关联的拥有方的变化,相反方的更改将会忽略,确保更新双向关联或者至少更新拥有方。




参考网址:http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html

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.

11.1. Bidirectional Associations

The following rules apply to bidirectional associations:

  • The inverse side has to use the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. The mappedBy attribute contains the name of the association-field on the owning side.

  • The owning side has to use the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. The inversedBy attribute contains the name of the association-field on the inverse-side.

  • ManyToOne is always the owning side of a bidirectional association.

  • OneToMany is always the inverse side of a bidirectional association.

  • The owning side of a OneToOne association is the entity with the table containing the foreign key.

  • 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 his 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.

“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.



by 雪洁 2015-12-25 04:08:09 2545 views
我来说几句

相关文章