简体   繁体   中英

doctrine bidirectional one-to-one relation

I know that in symfony2/doctrine, in order to create the bidirectional relationship, we have to tell that into the annotations of the two entities.

What I need though, is to create this bidirectional relationship by modifying only the annotations of only one entity, which means in the other entity annotations there is nothing about the other one.

Is this possible?

What you describe is unidirectional association, the documentation: http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html?highlight=bidirectional states following rules to achieve bidirectional association:

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.

So I can't be sure if that's possible, but in any case that would probably be breaking the rules, which would possibly cause some errors. Why do you need this kind of association anyway?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM