在Doctrine 2中多对多选择?

时间:2015-07-28 13:30:13

标签: php symfony orm doctrine-orm dql

我无法做出多对多关系的选择功能。

我有三张桌子

用户和字段ID,姓名,电子邮件...
系统和字段id,名称,认证
user_system和字段id_user,id_system,access ...

使用doctrine控制台,他给我带来了两个实体,User实体和实体System

class User{

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=45, nullable=false)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=60, nullable=false)
 */
private $email;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="date_create", type="datetime", nullable=false)
 */
private $dateCreate;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Procob\AutenticacaoBundle\Entity\System", inversedBy="idUser")
 * @ORM\JoinTable(name="user_system",
 *   joinColumns={
 *     @ORM\JoinColumn(name="id_user", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="id_system", referencedColumnName="id")
 *   }
 * )
 */
private $system;

/**
 * Constructor
 */
public function __construct()
{
    $this->system = new \Doctrine\Common\Collections\ArrayCollection();
}


/**
 * Set name
 *
 * @param string $name
 * @return Usuario
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}


/**
 * Set email
 *
 * @param string $email
 * @return Usuario
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string 
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set dateCreate
 *
 * @param \DateTime $dateCreate
 * @return Usuario
 */
public function setDateCreate($dateCreate)
{
    $this->dateCreate = $dateCreate;

    return $this;
}

/**
 * Get dateCreate
 *
 * @return \DateTime 
 */
public function getDateCreate()
{
    return $this->dateCreate;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Add system
 *
 * @param \Procob\AutenticacaoBundle\Entity\System $system
 * @return Usuario
 */
public function addSystem(\Procob\AutenticacaoBundle\Entity\System $system)
{
    $this->system[] = $system;

    return $this;
}

/**
 * Remove system
 *
 * @param \Procob\AutenticacaoBundle\Entity\System $system
 */
public function removeSystem(\Procob\AutenticacaoBundle\Entity\System $system)
{
    $this->system->removeElement($system);
}

/**
 * Get system
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getSystem()
{
    return $this->system;
}
}




class System{

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=45, nullable=false)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="authentication", type="string", length=10, nullable=false)
 */
private $authentication;

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Procob\authenticationBundle\Entity\User", mappedBy="system")
 */
private $idUser;

/**
 * Constructor
 */
public function __construct()
{
    $this->idUser = new \Doctrine\Common\Collections\ArrayCollection();
}


/**
 * Set name
 *
 * @param string $name
 * @return system
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set authentication
 *
 * @param string $authentication
 * @return system
 */
public function setAuthentication($authentication)
{
    $this->authentication = $authentication;

    return $this;
}

/**
 * Get authentication
 *
 * @return string 
 */
public function getAuthentication()
{
    return $this->authentication;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Add idUser
 *
 * @param \Procob\authenticationBundle\Entity\User $idUser
 * @return system
 */
public function addIdUser(\Procob\authenticationBundle\Entity\User $idUser)
{
    $this->idUser[] = $idUser;

    return $this;
}

/**
 * Remove idUser
 *
 * @param \Procob\authenticationBundle\Entity\User $idUser
 */
public function removeIdUser(\Procob\authenticationBundle\Entity\User $idUser)
{
    $this->idUser->removeElement($idUser);
}

/**
 * Get idUser
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getIdUser()
{
    return $this->idUser;
}
}

如何在创建两个实体的情况下返回三个表的值? 我阅读了文档,但没有。

1 个答案:

答案 0 :(得分:2)

ManyToMany关联应该仅用于两个实体之间的关系,而没有关于此关系的额外信息。

如果您想在关系中存储额外信息,请创建单独的实体UserSystem,并向ManyToOneUser实体添加System关联。

然后你可以使用下一个dql来获取系统及其关系的所有用户:

SELECT u, s, us
FROM   AppBundle:User u
JOIN   u.userSystems us
JOIN   us.system s