Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the owning side and inverse side in the doctrine 2 doc example

On this page of association mapping, there's an example in the manytomany section. But I don't understand which entity (group or user) is the owning side.

http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-bidirectional

I've put the code here too

<?php
/** @Entity */
class User
{
    // ...

    /**
     * @ManyToMany(targetEntity="Group", inversedBy="users")
     * @JoinTable(name="users_groups")
     */
    private $groups;

    public function __construct() {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

/** @Entity */
class Group
{
    // ...
    /**
     * @ManyToMany(targetEntity="User", mappedBy="groups")
     */
    private $users;

    public function __construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

Do I read this annotation like this: User is mappedBy groups so group is the entity that does the connection management, thus the owning side?

Also, I've read this in the docs:

For ManyToMany bidirectional relationships either side may be the owning side (the side  that defines the @JoinTable and/or does not make use of the mappedBy attribute, thus using   a default join table).

This lets me think that User would be the owning side as the JoinTable annotation is defined in that entity.

like image 346
mattyh88 Avatar asked Nov 12 '12 15:11

mattyh88


1 Answers

But I don't understand which entity (group or user) is the owning side

The User entity is the owner. You have the relation of groups in User:

/**
 * @ManyToMany(targetEntity="Group", inversedBy="users")
 * @JoinTable(name="users_groups")
 */
private $groups;

Look above, $groups var contains the all groups associated to this user, but If you notice the property definition, $groups var has the same name of mappedBy value (mappedBy="groups"), as you did:

/**
 * @ManyToMany(targetEntity="User", mappedBy="groups")
 */
private $users;

What does mappedBy mean?

This option specifies the property name on the targetEntity that is the owning side of this relation.

like image 132
manix Avatar answered Sep 30 '22 18:09

manix