Marlyyy
Marlyyy

Reputation: 722

Symfony2 - ManyToMany - Can't update owning side only

I have a ManyToMany relationship between User table and Role table. User table is the owning side. My problem is that when I persist data into the User table, the "user_role" table (which is the helper table of the relationship) won't update.

AccountController.php:

$user = $registration->getUser(); // all the user data works

$em->persist($user);
$em->flush();

User.php:

/**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     * @ORM\JoinTable(name="user_role",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     *
     */

    private $roles;

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

    // trying to assign this role by default
    public function getRoles()
    {
        return array('ROLE_ADMIN');
    }

    public function addRole(Role $role) {
        $this->roles[] = $role;
        $role->addUser($this);

        return $this;
    }

Role.php:

/**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
     * @ORM\JoinTable(name="user_role",
     *      joinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
     * )
     */
    private $users;

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

    public function getRole()
    {
        return $this->role;
    }

    public function setRole($role)
    {
        $this->role = $role;
    }


    public function addUser(User $users)
    {
        $this->users[] = $users;

        return $this;
    }

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

I realize I'm not even using addRole in the controller. But for that, I'd have to persist the Role as well (otherwise fatal error), and if I do, then it would be a duplicate entry inside the Role table.


Basically my question in brief: how do I save a new User in the database, while updating the user_role table, without adding/duplicating a new Role?

Any help would be really appreciated.

Upvotes: 1

Views: 213

Answers (1)

Marlyyy
Marlyyy

Reputation: 722

Okay, it's been solved!

Basically the reason why it kept trying to duplicate the entry in Role table is because I created a new entity and tried to add that as a role. Here's the bad code:

$role = new Role();
$role->setName('admin');
$role->setRole('ROLE_ADMIN');
$user->addRole($role);

And here's what I should have done: fetch the existing role I wanted to assign from the database:

$role = $em->getRepository('NameYourBundle:Role')->findOneBy(array('name' => 'admin'));
$user->addRole($role);
$em->persist($user); // persisting only the user. 
$em->flush();

After adding this existing role, the helper table will be updated accordingly!

Upvotes: 2

Related Questions