public function load(ObjectManager $manager) { for ($i = 1; $i <= 5; $i++) { $comment1 = new BookComment(); $comment1->setName('Євгеній'); $comment1->setComment('Відміна книга'); $comment1->setBook($this->getReference("book-" . $i)); $comment2 = new BookComment(); $comment2->setName('Юлія'); $comment2->setComment('Усім рекомендую цю кнжику. Авто чітко розкриває усі нюанси'); $comment2->setBook($this->getReference("book-" . $i)); $comment3 = new BookComment(); $comment3->setName('Марія'); $comment3->setComment('5 з 5'); $comment3->setBook($this->getReference("book-" . $i)); $comment4 = new BookComment(); $comment4->setName('Ірина'); $comment4->setComment('Краще, що я читала за останній час'); $comment4->setBook($this->getReference("book-" . $i)); $manager->persist($comment1); $manager->persist($comment2); $manager->persist($comment3); $manager->persist($comment4); } $manager->flush(); }
/** * @Method("POST") * @Route("/book-comments/{book}", name="book_comment_create") */ public function createAction(Book $book) { $comment = new BookComment(); $comment->setBook($book); $form = $this->createForm(new BookCommentType(), $comment); $form->submit($this->getRequest()); if ($form->isValid()) { $em = $this->getManager(); $em->persist($comment); $em->flush(); return $this->redirectToRoute('book_detail', ['id' => $comment->getBook()->getId()]); } //TODO output errors }