Я использую symfony 2.8, я создал отношения «один ко многим» между пользователем и блогом (один пользователь — много блогов). Также я создал связь «один ко многим» между блогом и комментариями. Точно так же отношение «один ко многим» между пользователем и комментариями. Теперь моя таблица комментариев выглядит так
таблица комментариев
id Primary int(11)
user_id Primary int(11)
blog_id Primary int(11)
comment varchar(2000)
created_at datetime
Объект комментария
Блог
Объект пользователя
Контроллер блога -> метод ShowAction
/**
* @Route("/blog/show/{id}", name="blog_show")
*/
public function showAction(Request $request,$id)
{
$blog = $this->getDoctrine()->getRepository(Blog::class)->findOneById($id);
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = new User();
$blog = new Blog();
$comment->setUser($this->getUser());
$comment->setBlog($blog);
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
return $this->redirect($this->generateUrl('blog_show', array('id' => $id)), 301);
}
return $this->render('Blog/show.html.twig', array('blog'=> $blog,'form' => $form->createView()));
}
Теперь, когда я отправляю форму комментария со страницы показа блога, она показывает мне эту ошибку
"A new entity was found through the relationship 'AppBundle\Entity\Comment#blog' that was not configured to cascade persist operations for entity: AppBundle\Entity\Blog@0000000017546b06000000001fededdf. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'AppBundle\Entity\Blog#__toString()' to get a clue."
Что я пропустил здесь. Буду признателен за любую оказанную помощь. Спасибо
getBlog
. Это правда? - person Ben Hillier   schedule 27.09.2017Comment
. Когда вы вызываете$this->getBlog();
, он ищет эту функцию в самомBlogController
. Вам нужно получить его из формы вместо этого? - person Ben Hillier   schedule 27.09.2017