doctrine owning side and inverse side

owning and inverse side can be confusing. Then there is uni and bi directional relationships.

For example:

    /**
     * @ORM\ManyToMany(targetEntity="\Acme\UserBundle\Entity\User", inversedBy="users")
     * @ORM\JoinTable(
     *     name="gallery_has_users",
     *     joinColumns={@ORM\JoinColumn(name="gallery_id", referencedColumnName="id", nullable=false)},
     *     inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)}
     * )
     */
    private $users;

These are some key rules:

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

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

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

How do you decide on the owning side?

This sentence helps:

Entity A(Owning) has Entity B(inverse).
If I delete Entity A, I want to delete Entity B.

Doctrine will only check the owning side of an association for changes.

refer to doctrine site for more info.

http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

Like it.? Share it:

Comments are closed.