【问题标题】:updating data in symfony在 symfony 中更新数据
【发布时间】:2017-06-22 11:22:19
【问题描述】:

我只想问你是否可以帮助我完成我在 symfony2 中的任务。我正在开发一个评论部分,就像您可以在模式的文本框中写评论一样,然后单击“添加评论”,您写的评论将存储在名为 edi_864824 的表中,它位于名为的列中“cmets”在其对应的 id (ediTransactionId) 中。但我只是 symfony2 框架的新手,这就是为什么我真的很难做到这一点。请帮我更正我的代码。 谢谢

这是我的路线:

matrix_edi_addComment:
pattern: /addComment/{id}-{url}
defaults: { _controller: "MatrixEdiBundle:Matrix:addComment" }

这是我的控制器:

public function addCommentAction($id, $url, Request $request) {

$em = $this->getDoctrine()->getManager();

$trans = $em->getRepository('MatrixEdiBundle:Edi864824')->findOneByEdiTransactionId($id);

$comment = $this->get('request')->request->get('comment');

$trans->setComment($comment);

$em->persist($trans);

$em->flush();

$session = $this->getRequest()->getSession();
$session->getFlashBag()->add('message', 'Your comment has been saved.');

return $this->redirect($this->generateUrl($url));

}

这是我的存储库:

public function getOutputComment($transDate){

    $em = $this->getEntityManager();

    $query = $em->createQuery(

        'SELECT partial a.{ediTransactionId, transactionDate, isaNumber, gsNumber, fileName},
                partial b.{ediTransactionId, tradingPartner, transDate, issue, referenceNumberId, comment},
                partial c.{referenceNumberId, transCn, poNumber, asnNumber}
         FROM MatrixEdiBundle:EdiTransaction a
         JOIN a.edi864824 b
         JOIN b.referenceNumber c
         WHERE a.ediTransactionId = b.ediTransactionId
         AND b.referenceNumberId = c.referenceNumberId
         AND b.transDate LIKE :transDate
         ORDER BY b.transDate DESC')

    ->setParameter('transDate', "%$transDate%");

    return $query->getResult();

}

这是在另一个页面的 twig 查询 (inputComment.html.twig) 中,模态框所在的其他页面用于写入评论并重定向到具有评论列的新 twig 查询 (outputComment.html.twig)在里面。

<a data-href="{{ path('matrix_edi_addComment', { 'id':trans.ediTransactionId,'url': 'matrix_edi_outputComment' }) }}" data-toggle="modal" data-target="#confirm-delete"><center><i class="fa fa-comment" style="color: #1975A3;"></i></center></a>

【问题讨论】:

  • 如果它适合你,请接受我的回答:)

标签: php symfony


【解决方案1】:

我感觉你的代码应该是这样的:

public function addCommentAction($ediTransactionId, Request $request){

   $em = $this->getDoctrine()->getManager();

   $trans = $em->getRepository('MatrixEdiBundle:ediTransaction')->findOneBy($ediTransactionId);

   $comment = $request->get("comment"); // <-- I changed this line
   $trans->setComment($comment); // <-- I changed this line
   $em->persist($trans);
   $em->flush();

   $session = $this->getRequest()->getSession();
   $session->getFlashBag()->add('message', 'Your comment has been saved.');

   return $this->redirect($this->generateUrl('matrix_edi_outputComment'));     

}

PS,您不需要 $em-&gt;persist($trans);,因为 $trans 对象已经在 Doctrine 中注册(因为您之前已获取它)

【讨论】:

  • Richard,你能重新检查一下我的代码吗?我编辑了我的 Action 和 twig,以便更清晰地展示我的程序。请告诉我需要进行哪些更正。谢谢
  • 首先你要说什么是行不通的?另外,你应该向同事寻求帮助来设置 xdebug,然后你会更容易解决这些问题☺️
  • Controller "Matrix\MatrixEdiBundle\Controller\MatrixController::addCommentAction()" 要求您为 "$comment" 参数提供一个值(因为没有默认值或因为有一个非可选的在这之后的争论)。我从以下代码中得到了这个错误: $trans->setComment($comment);在您的更正中,您建议我应该更改那条线,我不知道实际上如何。对不起
  • 嗨,我更新了这个问题,请看一下...所有这些问题都在 Symfonys 文档中进行了深入记录,您应该仔细阅读并阅读它...
猜你喜欢
  • 1970-01-01
  • 2020-03-21
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多