Reputation: 7902
I am using Symfony (2.7.5) and Doctrine to manage my data.
I have set up a many to many relationship between users and groups and want to be able to updated this from both the user and group form.
I can only get it to save from the owning side.
User Entity:
/**
* @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;
}
User form;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('groups', 'entity', [
'class' => 'AppBundle:Group',
'choice_label' => 'name',
'expanded' => true,
'multiple' => true,
]);
}
Group Entity;
/**
* @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;
}
Group form;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('groups', 'entity', [
'class' => 'AppBundle:User',
'choice_label' => 'name',
'expanded' => true,
'multiple' => true,
]);
}
Upvotes: 0
Views: 378
Reputation: 59
For me to get my ManyToMany relationship working right I ended up just specifying everything in the mapping. So you might have something like...
// 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")}
* )
*/
and
// 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")}
* )
*/
Upvotes: 0
Reputation: 12721
Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view)
Upvotes: 1