为了显示特定图书的评论,您需要在Book 实体中实现OneToMany 关系(作为反面),并在Review 实体中实现ManyToOne 关系(作为拥有方)。
我已经搜索了完整的示例,但找不到任何示例,所以这是我的尝试:
1) 创建Book 和Review 实体,没有任何关系,并在数据库中创建它们。如果您尝试在关系旁边创建表,并且这些表尚未存在于数据库中,则会出现错误。
2) 现在您可以创建关系了
# AppBundle\Entity\Book.php
use Doctrine\Common\Collections\ArrayCollection;
...
class Book
...
/**
* @ORM\OneToMany(targetEntity="Review", mappedBy="book")
*/
private $reviews;
public function __construct()
{
$this->reviews = new ArrayCollection();
}
//... getters and setters for extra fields you might have
/**
* @return ArrayCollection|Review[]
*/
public function getReviews()
{
return $this->reviews;
}
// Notice here you don't need the setReviews() setter!
# AppBundle/Entity/Review.php
use AppBundle\Entity\Book;
...
class Review
...
/**
* @ORM\ManyToOne(targetEntity="Book", inversedBy="reviews")
*/
private $book;
//... getters and setters for extra fields you might have
/**
* @return Review
*/
public function getBook()
{
return $this->book;
}
/**
* Notice here is passed the entire book object
* @param Book $book
* @return $this
*/
public function setBook(Book $book)
{
$this->book = $book;
return $this;
}
3) 通过创建迁移或使用doctrine:schema:update --force 命令来应用这些关系。
4) 根据实体创建表单
-> BookType:php bin/console doctrine:generate:form AppBundle:Book
-> ReviewType:php bin/console doctrine:generate:form AppBundle:Review,您需要使用 EntityType 才能获得属于该评论数组的图书。
# AppBundle/Form/ReviewType.php
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
...
class ReviewType extends AbstractType
...
->add('book', EntityType::class, [
'class' => 'AppBundle:Book',
'placeholder' => ' ',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('b');
},
'choice_label' => function($book){
return $book->getId();// in the <select> options put the ids from Book entity
},
'multiple' => false, // a user can select only one option per submission
'expanded' => false // options will be presented in a <select> dropdown; set this to true, to present the data in checkboxes
])
5) 在控制器中创建和保存书籍的操作:
# AppBundle/Controller/DefaultController.php
use AppBundle\Entity\Book;
use AppBundle\Form\BookType;
...
/**
* @Route("/book", name="book")
*/
public function bookAction(Request $request)
{
$book = new Book();
$form = $this->createForm(BookType::class, $book, [
'action' => $this->generateUrl('book'),
'method' => 'POST'
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($book);
$em->flush();
return $this->redirectToRoute('book');
}
return $this->render('default/book.html.twig', ['form'=>$form->createView()]);
}
6) 在控制器中创建和保存评论的操作
# AppBundle/Controller/DefaultController.php
use AppBundle\Entity\Review;
use AppBundle\Form\ReviewType;
...
/**
* @Route("/review", name="review")
*/
public function reviewAction(Request $request)
{
$review = new Review();
$form = $this->createForm(ReviewType::class, $review, [
'action' => $this->generateUrl('review'),
'method' => 'POST'
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($review);
$em->flush();
return $this->redirectToRoute('review');
}
return $this->render('default/review.html.twig', ['form'=>$form->createView()]);
}
7) 用于显示特定书籍的评论:
# AppBundle/Controller/DefaultController.php
/**
* @Route("/", name="homepage")
*/
public function indexAction()
{
$reviews = $this->getDoctrine()->getRepository('AppBundle:Review')->findAll();
if (!$reviews) {
throw $this->createNotFoundException('No review(s) found!');
}
return $this->render('default/index.html.twig',['reviews'=>$reviews]);
}
8) 显示评论的视图(对于书籍和评论,只需打印表格,所以我不会发布代码,因为这很琐碎)
# app/Resources/views/default/index.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{% for review in reviews %}
{{ review.comment ~ '-' ~ review.book.name }}
{% endfor %}
{% endblock %}