脚本宝典收集整理的这篇文章主要介绍了php – Symfony doctrine repository(findOneBy)结果不是对象,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
/** * 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(),); }
现在我得到我的参数并尝试在学说中找到对象.
/** * 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',)); }
当我给出一个数字而不是我的请求参数时,一切正常.
仅针对第二种情况发生以下错误消息:
我不明白我做错了什么.
编辑:
并且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,); }
以上是脚本宝典为你收集整理的php – Symfony doctrine repository(findOneBy)结果不是对象全部内容,希望文章能够帮你解决php – Symfony doctrine repository(findOneBy)结果不是对象所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。