Advertisement
sanjiisan

Untitled

Aug 6th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5. use AppBundle\Entity\User;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use AppBundle\Entity\Book;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12.  
  13. class DefaultController extends Controller
  14. {
  15. /**
  16. * @Route("/newBook", name="new_book")
  17. * @return Response
  18. */
  19. public function newBookAction(Request $request)
  20. {
  21. $em = $this->getDoctrine()->getEntityManager();
  22. $repository = $em->getRepository('AppBundle:Author');
  23.  
  24. $authors = $repository->findAll();
  25.  
  26. return $this->render('@App/Default/form.html.twig', [
  27. 'authors' => $authors
  28. ]);
  29. }
  30.  
  31. /**
  32. * @Route("/createBook", name="create_book")
  33. */
  34. public function createBookAction(Request $request)
  35. {
  36. $book = new Book();
  37. $book->setDescription($request->request->get('description'));
  38. $book->setTitle($request->request->get('title'));
  39. $book->setRating($request->request->get('rating'));
  40.  
  41. $em = $this->getDoctrine()->getEntityManager();
  42. $repo = $em->getRepository('AppBundle:Author');
  43. $author = $repo->find($request->request->get('author'));
  44.  
  45. $book->setAuthor($author);
  46.  
  47. $em = $this->getDoctrine()->getEntityManager();
  48. $em->persist($book);
  49. $em->persist($author);
  50. $em->flush();
  51.  
  52. return $this->redirectToRoute('show_book', ['id' => $book->getId()]);
  53. }
  54.  
  55. /**
  56. * @Route("/showBook/{id}", name="show_book")
  57. */
  58. public function showBookAction($id)
  59. {
  60. $em = $this->getDoctrine()->getEntityManager();
  61. $repository = $em->getRepository('AppBundle:User');
  62.  
  63. $book = $repository->find($id);
  64.  
  65. return $this->render('@App/Default/render.html.twig', [
  66. 'book' => $book
  67. ]);
  68. }
  69.  
  70. /**
  71. * @Route("/showAllBooks")
  72. */
  73. public function showAllBooksAction()
  74. {
  75. $em = $this->getDoctrine()->getEntityManager();
  76. $repository = $em->getRepository('AppBundle:Book');
  77. $books = $repository->findAll();
  78.  
  79. return $this->render('@App/Default/renderAll.html.twig', [
  80. 'books' => $books
  81. ]);
  82. }
  83.  
  84.  
  85. /**
  86. * @Route("/deleteBook/{id}")
  87. */
  88. public function deleteBooksAction($id)
  89. {
  90. $em = $this->getDoctrine()->getEntityManager();
  91. $repository = $em->getRepository('AppBundle:Book');
  92.  
  93. $book = $repository->find($id);
  94. $em->remove($book);
  95. $em->flush();
  96.  
  97. return new Response('Usunięto!!');
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement