Symfony3错误使用AJAX更改实体的DateTime



我想更改学说实体的日期,但没有保存更改。

使用ajax a调用此功能:

public function relancerTicketAction(Request $request, $id)
{
    if (!$this->get('session')->get('compte'))
        return $this->redirect($this->generateUrl('accueil'));
    $isAjax = $request->isXMLHttpRequest();
    if ($isAjax)
    {
        $ticket = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Ticket')->find($id);
        $ticket->setDateButoire($ticket->getDateButoire()->modify('+7 day'));
        $this->getDoctrine()->getManager()->flush();
        $response = array("code" => 100, "success" => true, 'date' => $ticket->getDateButoire()->format('d-m-Y'));
        return new Response(json_encode($response));
    }
    $response = array("code" => 0, "success" => false);
    return new Response(json_encode($response));
}

当我提醒结果时,我得到了正确的新值,但是在重新加载后,没有保存更改。

在相同条件下调用此功能有效:

public function traiterTicketAction(Request $request, $id)
{
    if (!$this->get('session')->get('compte'))
        return $this->redirect($this->generateUrl('accueil'));
    $isAjax = $request->isXMLHttpRequest();
    if ($isAjax)
    {
        $compte = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Compte')->find($this->get('session')->get('compte')->getId());
        $ticket = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Ticket')->find($id);
        $ticket->addDestinataire($compte);
        $this->getDoctrine()->getManager()->flush();
        $response = array("code" => 100, "success" => true);
        return new Response(json_encode($response));
    }
    $response = array("code" => 0, "success" => false);
    return new Response(json_encode($response));
}

请参阅docs

调用EntityManager#flush()学说计算的更改 所有当前管理的实体,并将差异保存到 数据库。如果是对象属性(@column(type =" dateTime")或 @column(type ="对象"))这些比较始终由 参考。这意味着以下更改不会保存到 数据库:

/** @Entity */
class Article
{
    /** @Column(type="datetime") */
    private $updated;
    public function setUpdated()
    {
        // will NOT be saved in the database
        $this->updated->modify("now");
    }
}

所以,在您的情况下,我建议像这个

一样克隆datebutoire
$ticket = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Ticket')->find($id);
$newDateButoire = clone $ticket->getDateButoire();
$ticket->setDateButoire($newDateButoire->modify('+7 day'));
$this->getDoctrine()->getManager()->flush();

相关内容

  • 没有找到相关文章