私はsymfony 2.8を使用しています。私はユーザーとブログの間に1対多の関係を作成しました。また、私はブログとコメントの間に一対一の関係を作りました。同様に、ユーザーとコメントの1対多の関係。今、私のコメントの表は、このSymfonyユーザーとブログIDでブログにコメントを保存
コメントテーブルのように見えます
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\[email protected] 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'という関数はありません。本当? –
コメントエンティティをチェックすると、getBlog()が定義された関数が1つあります。しかし、それはまだそのエラーを示しています。 – Aamir
それは 'Comment'エンティティにあります。 $ this-> getBlog();を呼び出すと、 'BlogController'自体でその関数を探しています。代わりにフォームからそれを取得する必要がありますか? –