Search

N-N Unidirectional relation Doctrine 2 ORM


Description


use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User {
    /**
     * @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue
     * @var int
     */
    private $id;

    /**
     * Many Users have Many Groups.
     * @ORM\ManyToMany(targetEntity="Group")
     * @ORM\JoinTable(name="users_groups",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     *      )
     */
    private $groups;

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

/**
 * @ORM\Entity
 * @ORM\Table(name="group")
 */
class Group {
    /**
     * @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue
     * @var int
     */
    private $id;
}
Table user

- id: PK, Auto Increment

- parent_id: FK category(id)



Table users_groups

- user_id: PK, FK user(id)

- group_id: PK, FK group(id)



Table group

- id: PK, Auto Increment
SEE ALSO