Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundle\Controller;
- use AppBundle\Entity\User;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use AppBundle\Entity\Book;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- class DefaultController extends Controller
- {
- /**
- * @Route("/newBook", name="new_book")
- * @return Response
- */
- public function newBookAction(Request $request)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $repository = $em->getRepository('AppBundle:Author');
- $authors = $repository->findAll();
- return $this->render('@App/Default/form.html.twig', [
- 'authors' => $authors
- ]);
- }
- /**
- * @Route("/createBook", name="create_book")
- */
- public function createBookAction(Request $request)
- {
- $book = new Book();
- $book->setDescription($request->request->get('description'));
- $book->setTitle($request->request->get('title'));
- $book->setRating($request->request->get('rating'));
- $em = $this->getDoctrine()->getEntityManager();
- $repo = $em->getRepository('AppBundle:Author');
- $author = $repo->find($request->request->get('author'));
- $book->setAuthor($author);
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($book);
- $em->persist($author);
- $em->flush();
- return $this->redirectToRoute('show_book', ['id' => $book->getId()]);
- }
- /**
- * @Route("/showBook/{id}", name="show_book")
- */
- public function showBookAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $repository = $em->getRepository('AppBundle:User');
- $book = $repository->find($id);
- return $this->render('@App/Default/render.html.twig', [
- 'book' => $book
- ]);
- }
- /**
- * @Route("/showAllBooks")
- */
- public function showAllBooksAction()
- {
- $em = $this->getDoctrine()->getEntityManager();
- $repository = $em->getRepository('AppBundle:Book');
- $books = $repository->findAll();
- return $this->render('@App/Default/renderAll.html.twig', [
- 'books' => $books
- ]);
- }
- /**
- * @Route("/deleteBook/{id}")
- */
- public function deleteBooksAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $repository = $em->getRepository('AppBundle:Book');
- $book = $repository->find($id);
- $em->remove($book);
- $em->flush();
- return new Response('Usunięto!!');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement