我知道人们多次询问过这个问题,但我真的不知道如何解决它。
我认为控制器内的方法存在错误:
/**
* View action.
*
* @param Website $website Website entity
* @param Request $request
* @param \Symfony\Component\HttpFoundation\Request $request HTTP Request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response HTTP Response
* @internal param Rating $rating
* @Route(
* "/{id}",
* name="website_view",
* )
* @Method({"GET", "POST"})
*/
public function viewAction(Website $website, Request $request)
{
$websiteId = $this->get('app.repository.website')->findOneById($website)->getId();
$rating = new Rating();
$form = $this->createForm(RatingType::class, $rating);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$rating->setWebsite($websiteId);
$this->get('app.repository.rating')->save($rating, $website);
$this->addFlash('success', 'message.created_successfully');
}
return $this->render(
'website/view.html.twig',
['website' => $website,
'rating' => $rating,
'form' => $form->createView()]
);
有问题的一行是:
$rating->setWebsite($websiteId);
如果该行丢失,则不会将网站ID设置为评级,但是当添加该行时,我会遇到错误。
我在Rating实体中有必要的getter和setter,并在Rating Repository中使用persist和flush保存方法。
问题可能是我将此方法放在网站控制器而非评级控制器中吗?
提前感谢您的帮助。
答案 0 :(得分:0)
使用网站对象作为参数而不是网站ID
来调用此函数...
public function viewAction(Website $website, Request $request)
{
$websiteId = $website->getId();
...
$rating->setWebsite($website);
...
}