我有两个模型:Entity \ Announcement.php和Entity \ Submission.php。
在我的Entity \ Submission.php中,我像这样定义了与Entity \ Announcement.php的关系:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Announcement")
* @ORM\JoinColumn(nullable=false)
*/
protected $announcement;
当用户访问公告时,他们可以提交提案。
在我的控制器中,我定义了这样的提交动作:
/**
* @Route("/submit/{id}", name="submissions_new")
*/
public function submitAction(Request $request, Announcement $id)
{
$submission = new Submission();
$submission->setAnnouncement($id);
$form = $this->createForm(SubmissionType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$submission = $form->getData();
$em->persist($submission);
$em->flush();
return $this->redirectToRoute('announcements_list');
}
return $this->render(':submissions:submit.html.twig', [
'form' => $form->createView(),
'announcement' => $id->getId()
]);
}
当我尝试从我的路线/ submit / 73c67a07-744d-11e7-9670-08d40c1d4f7f提交时,我收到此错误:
An exception occurred while executing 'INSERT INTO reports (id, title, date, author, description, announcement_id) VALUES (?, ?, ?, ?, ?, ?)' with params ["ff28c231-7843-11e7-a72b-08d40c1d4f7f", "aaa", "2012-01-01 00:00:00", "aaa", "aaa", null]:
Integrity constraint violation: 1048 Column 'announcement_id' cannot be null
当我在控制器中评论此行时:
//$submission = $form->getData();
并尝试再次提交,我收到此错误:
An exception occurred while executing 'INSERT INTO reports (id, title, date, author, description, announcement_id) VALUES (?, ?, ?, ?, ?, ?)' with params ["d12ea15b-7843-11e7-a72b-08d40c1d4f7f", null, null, null, null, "73c67a07-744d-11e7-9670-08d40c1d4f7f"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null
您能帮忙提交所有字段吗?
谢谢
答案 0 :(得分:0)
你的$ id真的是你数据库中的对象吗?
/**
* @Route("/submit/{id}", name="submissions_new")
*/
public function submitAction(Request $request, int $id)
{
$em = $this->getDoctrine()->getManager();
$submission = new Submission();
$announcement = $em->getRepository(Announcement::class)->find($id);
if (!$announcement) {
return $this->redirectToRoute('announcements_list');
}
$submission->setAnnouncement($announcement);
$form = $this->createForm(SubmissionType::class, $submission);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($submission);
$em->flush();
return $this->redirectToRoute('announcements_list');
}
return $this->render(':submissions:submit.html.twig', [
'form' => $form->createView(),
'announcement' => $announcement->getId()
]);
}
答案 1 :(得分:0)
public function submitAction(Request $request, Announcement $id)
{
$form = $this->createForm(SubmissionType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$formData = $form->getData();
$submission = new Submission();
$submission->setAnnouncement($id);
//to fill the $submission with data from forms
//$submission->setTitle($formData['title']);
//...........
$em->persist($submission);
$em->flush();
return $this->redirectToRoute('announcements_list');
}
return $this->render(':submissions:submit.html.twig', [
'form' => $form->createView(),
'announcement' => $id->getId()
]);
}