gpt4 book ai didi

php - Symfony Doctrine 存储库 (findOneBy) 结果不是对象

转载 作者:可可西里 更新时间:2023-10-31 23:38:24 27 4
gpt4 key购买 nike

我尝试理解 Doctrine 方面的一些奇怪行为,例如:

我有一个简单的表单,并在最后创建表单的请求中发送参数。

/**
* Displays a form to create a new Comment entity.
*
* @Route("/saveComment/", name="comment_new")
* @Method("POST")
* @Template()
*/
public function newAction(Request $request)
{
$entity = new Comment();
$form = $this->createCreateForm($entity, $request);

return array(
'entity' => $entity,
'form' => $form->createView(),
);
}

现在我得到我的参数并尝试在 doctrine 中找到对象。

/**
* Creates a form to create a Comment entity.
*
* @param Comment $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Comment $entity, Request $request)
{
$em = $this->getDoctrine()->getManager();

$commentForUserRequest = $request->request->get('commentForUser');
$commentForUser = $em->getRepository('ApplicationSonataUserBundle:User')->findOneBy(array('username' => $commentForUserRequest ));

$auctionRequest = $request->request->getInt('auction');

$auction = $em->getRepository('AppBundle:Auction')->find(1); // **ALL FINE**
$auction = $em->getRepository('AppBundle:Auction')->find($auctionRequest); //**PROBLEM**

$auction->addComment($entity);
$entity->setAuction($auction);

$form = $this->createForm(new CommentType(), $entity, array(
'action' => $this->generateUrl('comment_create'),
'method' => 'POST',
));
}

当我给出一个数字而不是我的请求参数时,一切正常。只有第二种情况才会出现以下错误信息:

Error: Call to a member function addComment() on a non-object

我不明白我做错了什么。

编辑:

Comment form

When I try sumbint

和comment_create

/**
* Creates a new Comment entity.
*
* @Route("/comment/", name="comment_create")
* @Method("POST")
* @Template("AppBundle:Comment:new.html.twig")
*/
public function createAction(Request $request)
{

$entity = new Comment();

$form = $this->createCreateForm($entity, $request);
$form->handleRequest($request);

if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();

return $this->redirect($this->generateUrl('comment_show', array('id' => $entity->getId())));
}

return array(
'entity' => $entity,
'form' => $form->createView(),
);
}

最佳答案

$request->request->getInt('auction');

此行将检查 POST(未获取)请求变量“拍卖”。

您可以使用相同的操作来生成和处理表单提交。

关于php - Symfony Doctrine 存储库 (findOneBy) 结果不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34445591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com