/** * Create a new game */ public function create() { $game = new Game(); $randomWord = $this->getEntityManager()->getRepository('HangmanApiBundle:Word')->getRandomWord(); $game->setWord($randomWord); $game->setTriesLeft(11); $game->setStatus('busy'); $guess = json_encode(array()); $game->setCharactersGuessed($guess); $em = $this->getEntityManager(); $em->persist($game); $em->flush(); return $game; }
/** * Method creates a new game. * * @ApiDoc( * resource=true, * output={ * "class"="Hangman\Bundle\ApiBundle\Entity\Game" * }, * statusCodes={ * 201="The new game is created.", * 500="Application error." * } * ) */ public function createAction(Request $request) { $view = $this->view("", 201); $repository = $this->getDoctrine()->getRepository('HangmanApiBundle:Word'); $word = $repository->createQueryBuilder('q')->addSelect('RAND() as HIDDEN rand')->addOrderBy('rand')->setMaxResults(1)->getQuery()->getOneOrNullResult(); $game = new Game(); $game->setTriesLeft(11); $game->setWord($word->getWord()); $game->setStatus(Game::STATUS_BUSY); $em = $this->getDoctrine()->getManager(); $em->persist($game); $em->flush(); $view->setData(array("game" => $game)); return $this->handleView($view); }