public function removeUserFromGame(User $user, Game $game) { if ($game->isUserInGame($user)) { $em = $this->getEntityManager(); $player = $game->getPlayerByUserId($user->getId()); // note it is important that we remove the player from the game before deleting them so that game state is updated to reflect this change $game->removePlayer($player); $em->remove($player); $em->flush(); } }
/** * @Route("/secure/game-remove-player/{id}", requirements={"id" = "\d+"}, name="game_remove_player") */ public function removePlayerAction(Game $game, Request $request) { $em = $this->getDoctrine()->getManager(); //make sure only game players can remove themselves $user = $this->getUser(); if (!$game->isUserInGame($user)) { return $this->redirectToRoute('game_home'); } if ($request->isMethod('POST')) { $em->getRepository('AppBundle:Game')->removeUserFromGame($user, $game); $this->notifyPlayersOfGameUpdate($game); $this->addFlash('notice', 'You have been removed from this game'); return $this->redirectToRoute('game_view', array('id' => $game->getId()), 301); } // we should never get to this point }