教义2多对多 - 拯救双方

时间:2015-10-21 12:18:28

标签: php symfony doctrine-orm

我正在使用Symfony(2.7.5)和Doctrine来管理我的数据 我在用户和组之间建立了多对多的关系,并希望能够从用户和组表单中更新它。
我只能从拥有方那里得到它。

用户实体:

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

/**
* Add group
*
* @param \AppBundle\Entity\Group $group
*
* @return User
*/
public function addGroup(\AppBundle\Entity\Group $group)
{
    if ($this->groups->contains($group)) {
        return;
    }
    $this->groups->add($group);
    $group->addUser($this);
}

/**
* Remove group
*
* @param \AppBundle\Entity\Group $group
*/
public function removeGroup(\AppBundle\Entity\Group $group)
{
    if (!$this->groups->contains($group)) {
        return;
    }
    $this->groups->removeElement($group);
    $group->removeUser($this);
}

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

用户表单;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('groups', 'entity', [
                'class' => 'AppBundle:Group',
                'choice_label' => 'name',
                'expanded' => true,
                'multiple' => true,
        ]);
}

集团实体;

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

/**
* Add user
*
* @param \AppBundle\Entity\User $user
*
* @return Group
*/
public function addUser(\AppBundle\Entity\User $user)
{
    if ($this->users->contains($user)) {
        return;
    }
    $this->users->add($user);
    $user->addGroup($this);
}

/**
* Remove user
*
* @param \AppBundle\Entity\User $user
*/
public function removeUser(\AppBundle\Entity\User $user)
{
    if (!$this->users->contains($user)) {
        return;
    }
    $this->users->removeElement($user);
    $user->removeGroup($this);
}

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

小组表格;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('groups', 'entity', [
                'class' => 'AppBundle:User',
                'choice_label' => 'name',
                'expanded' => true,
                'multiple' => true,
        ]);
}

2 个答案:

答案 0 :(得分:1)

  

忽略仅对关联的反面进行的更改。   确保更新双向关联的两侧(或在   至少是拥有方,从学说的角度来看)

来源:http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html

答案 1 :(得分:0)

为了让我的ManyToMany关系正常工作,我最终只是在映射中指定了所有内容。所以你可能会有类似......

 // src/AppBundle/Entity/Users.php
 /**
 * @Var group
 * @ORM\ManyToMany(targetEntity="Group", fetch="EAGER", cascade={"persist"}, inversedBy="users")
 * @ORM\JoinTable(name="users_groups",
 *      joinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="group_id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="user_id")}
 * )
 */

    // src/AppBundle/Entity/Group.php
         /**
 * @Var Users
 * @ORM\ManyToMany(targetEntity="Users", fetch="EAGER", cascade={"persist"}, inversedBy="group")
 * @ORM\JoinTable(name="users_groups",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="user_id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="group_id")}
 * )
 */