0

I have a problem, while deleting an entry in a m:n-table. I tried this solution, but I get the error: "ContextErrorException: Catchable Fatal Error: Argument 1 passed to Pso\ProjectBundle\Entity\Project::removeUser() must be an instance of Pso\LogBundle\Entity\User, string given, called in C:\xampp\htdocs\pso\src\Pso\ProjectBundle\Controller\ProjectController.php on line 59 and defined in C:\xampp\htdocs\pso\src\Pso\ProjectBundle\Entity\Project.php line 452"

I have a Class Project with the function:

/**
 * Remove users
 *
 * @param \Pso\LogBundle\Entity\User $users
 */
public function removeUser(\Pso\LogBundle\Entity\User $users)
{
    $this->users->removeElement($users);

}

Now I want to delete an entry from the table project_user. This table implements the n:m-relationship from user and project

Im my Controller I tried to adapt the linked solution:

public function deleteUserProjectAction($id, $projectid, Request $request) 
{
    $em = $this->getDoctrine()->getManager();
    $project = $em->find('PsoProjectBundle:Project', $projectid);

    $project->removeUser($id);
    $em->persist($project);
    $em->flush();
}

$id is the id of the user in the n:m table and $projectid the related project. Any hint for solution would be appreciated.

Community
  • 1
  • 1
Micha
  • 43
  • 7

1 Answers1

0

The error is extremly explicit. You are giving to the removeUser() function a string which is the user's id whereas the expected parameter type is a User.

You must retrieve the user from the DB thanks to $id and then pass this user to the function.

Grégory Elhaimer
  • 2,731
  • 1
  • 16
  • 21