Symfony2: persist many-to-many relation from inverse side with standard crud

4.5k Views Asked by At

I am wondering about the default behavior of generated many-to-many relations: While the relation is stored in the association table correctly when persisting the owning side of the relation, the persisting of the inversed side does not updating the relation table in the db.

I generated 2 CRUDs by simfony2 which work well so far at least with this "problem". I read some threads and the doctrine2 docs but did not got any further.

Also tried to use cascade={"persist"} on one and both sides but still no luck.

Any help would be great:

Here is my User entity:

/**
 * 
 *
 * @ORM\ManyToMany(targetEntity="SSV\AppBundle\Entity\Role", inversedBy="users")
 * @ORM\JoinTable(name="users_roles",
 *   joinColumns={
 *     @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="role_id", referencedColumnName="id")
 *   }
 * )
 */
private $roles;


/**
 * Add role
 *
 * @param \SSV\AppBundle\Entity\Role $role
 */
public function addRole(SSV\AppBundle\Entity\Role $role)
{
    $this->roles[] = $role;
}

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

and my role entity:

/**
 * 
 *
 * @ORM\ManyToMany(targetEntity="SSV\AppBundle\Entity\User", mappedBy="roles")
 */
private $users;


/**
 * Add user
 *
 * @param SSV\AppBundle\Entity\User $user
 */
public function addUser(SSV\AppBundle\Entity\User $user)
{
    $this->users[] = $user;
}

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

The Controller classes are not yet modified as I was sure to get it working with default behavior! Am I wrong with this?

Please help me out!

0

There are 0 best solutions below