Doctrine 2 - ManyToMany关系 - 空集合

时间:2016-08-16 08:45:00

标签: symfony doctrine-orm many-to-many

我在用户和群组之间有很多关系,但是当我想为用户访问所有群组时,我会收到空​​的收藏。

namespace LoginBundle\Entity;

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

/**
 * @ORM\Entity
 * @ORM\Table(name="User")
 */
class User
{
    /**
     * @ORM\Column(type="integer", name="id")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $_iId;

    /**
     * @ORM\Column(type="string", name="login", length=45)
     */
    private $_sLogin;

    /**
     * @ORM\ManyToMany(targetEntity="GroupBundle\Entity\Group", inversedBy="_aUser")
     * @ORM\JoinTable(name="Group_x_User",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     *      )
     */
    private $_aGroup;

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

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

    /**
     * Set sLogin
     *
     * @param string $sLogin
     *
     * @return User
     */
    public function setLogin($sLogin)
    {
        $this->_sLogin = $sLogin;

        return $this;
    }

    /**
     * Get sLogin
     *
     * @return string
     */
    public function getLogin()
    {
        return $this->_sLogin;
    }

    public function getGroups()
    {
        return $this->_aGroup;
    }

用户和组使用Group_x_User表来存储他们的关系。

namespace GroupBundle\Entity;

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

/**
 * @ORM\Entity
 * @ORM\Table(name="Group")
 */
class Group
{
    /**
     * @ORM\Column(type="integer", name="id")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $_iId;

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

    /**
     * @ORM\ManyToMany(targetEntity="LoginBundle\Entity\User", mappedBy="_aGroup")
     */
    private $_aUser;

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

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

    /**
     * Set sName
     *
     * @param string $sName
     *
     * @return Group
     */
    public function setName($sName)
    {
        $this->_sName = $sName;

        return $this;
    }

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

    public function getUsers()
    {
        return $this->_aUser;
    }
}

为了从数据库恢复数据,我使用代码:

$oUser = $this->getDoctrine()
        ->getRepository('LoginBundle:User')
        ->find(2);
$aGroup = $oUser->getGroups();

不幸的是,$ aGroup集合包含0个元素的数组,而在数据库中则是匹配的记录。我的映射中缺少什么?

1 个答案:

答案 0 :(得分:0)

User课程中缺少某些内容。您只能使用_aGroup声明ArrayCollection。这还不够。您必须将数据存储到ArrayCollection

将其添加到User班。

public class addGroups(\GroupBundle\Entity\Group $group)
{
    $group->addUsers($this);
    $this->_aGroup->add($group);
}

这适用于Group类。

public class addUsers(\LoginBundle\Entity\User $user)
{
    if (!$this->_aUser->contains($user)) {
        $this->_aUser->add($user);
    }
}

有关详细信息,请访问此link