多对多关系学说

时间:2015-02-05 17:04:58

标签: php symfony doctrine-orm

我当前的实体映射存在很多问题,所以我想就它的问题提出意见。

让我们说下面的示例包含Doctrine,但请记住可以有多个用户,以及一个用户可以是许多群组

的一部分

用户

ID

名称

ID

标题

用户组

ID

USER_ID

GROUP_ID

这是我目前的做法:

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\UserRepository")
 */
class User
{

    /**
     * @var integer
     * @ORM\Id
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $user_id;
 // some other fields and functions here
}

/**
     * Group
     *
     * @ORM\Table(name="group", indexes={@ORM\Index(name="group_parent", columns={"parent_id"})})
     * @ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\GroupRepository")
     */
    class Group
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="category_id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $categoryId;
    // some other functions and variables here
}

...和user_groups

/**
 * User_Groups
 *
 * @ORM\Table(name="user_groups",
 *             indexes={
 *                      @ORM\Index(name="user_group_user", columns={"user_id"}),
 *                      @ORM\Index(name="user_group_group", columns={"group_id"})
 *                     }
 * )
 * @ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\UserGroupRepository")
 */
class UserGroup
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(name="user_group_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $userGroupId;

      /**
     * @var \SomeBundle\Entity\User
     * @ORM\Column(name="user_id")
     *
     * @ORM\ManyToMany(targetEntity="SomeBundle\Entity\User", inversedBy="userGroups")
     * @ORM\JoinTable(name="users",
     * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="userId")})
     */

    private $user;

    /**
     * @var SomeBundle\Entity\Category
     *
     * @ORM\ManyToOne(targetEntity="SomeBundle\Entity\Group", inversedBy="groups")
     * @ORM\JoinColumn(name="group_id", referencedColumnName="group_id")
     *
     */
    private $group;
/// etc
}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

在这个例子中,我有一个product和一个mesureUnit,它们涉及很多很多。 在您的情况下,您只需要usergroupUserGroups会自动生成。 许多关系的完成如下:

<?php
namespace TeamERP\StoresBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 */
class MesureUnit
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=100, nullable=true)
 */
private $name;

/**
 * @ORM\Column(type="string", length=10, nullable=true)
 */
private $abreviation;

/**
 * @ORM\OneToMany(targetEntity="TeamERP\StoresBundle\Entity\ProductActivity", mappedBy="mesureUnit")
 */
private $pproductActivity;

/**
 * @ORM\ManyToMany(targetEntity="TeamERP\StoresBundle\Entity\Product", mappedBy="mesureUnit")
 */
private $product;
/**
 * Constructor
 */
public function __construct()
{
    $this->pproductActivity = new \Doctrine\Common\Collections\ArrayCollection();
    $this->product = new \Doctrine\Common\Collections\ArrayCollection();
}

然后produc类可能是:

<?php
namespace TeamERP\StoresBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 */
class Product
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=50, nullable=true)
 */
private $code;

/**
 * @ORM\Column(type="string", length=250, nullable=true)
 */
private $description;

/**
 * @ORM\Column(type="datetime", nullable=true)
 */
private $date_received;

/**
 * @ORM\OneToMany(targetEntity="TeamERP\StoresBundle\Entity\ProductActivity", mappedBy="product")
 */
private $pproductActivity;

/**
 * @ORM\ManyToOne(targetEntity="TeamERP\StoresBundle\Entity\Category", inversedBy="product")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
private $category;

/**
 * @ORM\ManyToOne(targetEntity="TeamERP\StoresBundle\Entity\AJob", inversedBy="product")
 * @ORM\JoinColumn(name="job_id", referencedColumnName="id")
 */
private $aJob;

/**
 * @ORM\ManyToMany(targetEntity="TeamERP\StoresBundle\Entity\MesureUnit", inversedBy="product")
 * @ORM\JoinTable(
 *     name="MesureUnit2Product",
 *     joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)},
 *     inverseJoinColumns={@ORM\JoinColumn(name="mesure_unit_id", referencedColumnName="id", nullable=false)}
 * )
 */
private $mesureUnit;
/**
 * Constructor
 */
public function __construct()
{
    $this->pproductActivity = new \Doctrine\Common\Collections\ArrayCollection();
    $this->mesureUnit = new \Doctrine\Common\Collections\ArrayCollection();
}

哟不需要手动创建表。它由Doctrine生成。

希望有所帮助

答案 1 :(得分:1)

/** @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();
    }

    // ...
}

您的案例是多对多,双向关联 Doctrine association mapping