【问题标题】:Symfony doctrine repository (findOneBy) result is not objectSymfony 学说存储库(findOneBy)结果不是对象
【发布时间】:2015-12-24 00:09:50
【问题描述】:

我尝试了解 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(),
    );
}

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

/**
 * 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',
    ));
}

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

错误:在非对象上调用成员函数 addComment()

我不明白我做错了什么。

编辑:

和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') 有回报吗?
  • @WojtekDmyszewicz 尝试单击发送按钮时存在问题。我添加了图片和更多细节。可以再检查一下吗?
  • @Bart 为什么不做 (int) $request->request->get('auction')。不是你总是可以使用 $form->getData();获取表单数据。
  • Doctrine 返回一个对象或null,如果没有找到。所以我 99% 确定您的请求中的数据或您提取该数据的方式有问题。尝试调试东西,查看变量的值等。
  • 调试 $auctionRequest 以查看在此行之后来自它的值 $auctionRequest = $request->request->getInt('auction');

标签: php symfony doctrine-orm doctrine


【解决方案1】:
$request->request->getInt('auction');

这一行将检查 POST (NOT GET) 请求变量“拍卖”。

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

【讨论】:

  • 谢谢,我构建了表单并发送了 POST 请求。工作正常!
猜你喜欢
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2018-08-15
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多