3

I have a schema with a Many-to-Many relationship between entities "User" and "Role" mapped as follows.

Role entity

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="roles", cascade={"persist", "remove"})
 */
protected $users;

and the User entity

/**
 * @ORM\ManyToMany(targetEntity="Role", inversedBy="users", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="users_roles")
 * 
 * @var ArrayCollection $userRoles
 */
protected $userRoles;

When I try to delete a role object with the following code,

$role = $em->getRepository('ACMEDefaultBundle:Role')->find($id);
$em->remove($role);
$em->flush();

I am getting a Doctrine Exception

ErrorException: Notice: Undefined index: roles in /media/sf_sandbox/aalcodev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 964 (uncaught exception) at /media/sf_sandbox/aalcodev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 964

Please help. Thank you.

Gopi Kalyan
  • 129
  • 2
  • 13
  • please do not use these words in titles: 'Problem'. See [Writing Good Titles](http://meta.stackexchange.com/questions/10647/writing-good-titles/10648#10648) – hakre Oct 18 '12 at 13:12
  • Which part of the error message is hard for you to understand? Where is the index called roles? – hakre Oct 18 '12 at 13:14
  • @hakre Sorry. First time posting here, thanks for the link. I don't have an index "roles". When I run 'schema:update", I get three tables created - users, roles and users_roles. I can add and update 'roles' just fine with the EntityManager. I get the error when I try the delete. I am also able to do the delete with Doctrine's DBAL. Am I missing something? – Gopi Kalyan Oct 18 '12 at 13:29
  • Yes, probably. The error you see is likely a standard PHP notice converted into an [`ErrorException`](http://php.net/ErrorException). Please see: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836 for a first introduction to the error. As this happens in the doctrine codebase, check the exact doctrine version you're using and add it to your question for further info. – hakre Oct 18 '12 at 13:48

1 Answers1

3

You should change mappedBy="roles" to mappedBy="userRoles". "Mapped by" should point to other side's property name...

Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85