调用findAll()方法后,在DB中取消最旧的raw

时间:2016-10-07 14:40:10

标签: symfony doctrine

我有一个用户密码恢复请求表。这些是他的专栏:

  • ID
  • ID用户所
  • createdDate
  • expiryDate
  • 令牌
  • isExpired

如果用户尝试发出多个请求,我想查找具有其ID的所有行,如果他们是> 1,将它们设置为 var dropdown = document.getElementById("qtySelect-61224b70-7b26-11e6-91d5-6921d6fe7421"); var selText = dropdown.options[dropdown.selectedIndex].text; 。 这样做,我想只考虑最后一个请求。

我怎样才能做到这一点?

这就是我现在这样做的方式,假设表中只有一行带有他的ID:

isExpired = true

如果我使用的话,请考虑我有3行具有相同的idUser:

if ($request->isMethod('POST')) {

    $form->handleRequest($request);
    $newPassword = $form->get('password')->getData();

    $currentToken = $request->query->get('token'); 

    $em = $this->getDoctrine()->getManager();
    $passwRecovery = $em->getRepository('\UserBundle\Entity\PasswordRecovery')->findOneBy(array('token' => $currentToken));

    if (empty($passwRecovery)) {

        return new Response ("<h3>Access denied!</h3>");
    }

    else {

    if (new \DateTime('now') > $passwRecovery->getExpireDate())
    {

        return new Response ("<h3>Token expired!</h3>");
    }

    $idUser = $passwRecovery->getIdUser();

    $userRecoverPassw = $em->getRepository('\UserBundle\Entity\User')->findOneBy(array('id' => $idUser));

    $userRecoverPassw->setPassword($newPassword);

    $em->persist($userRecoverPassw);
    $em->flush();

    return new Response ("<h3>Password reset ok!</h3>"); }

我会得到3个结果。如何仅为前两个设置 $passwRecovery = $em->getRepository('\UserBundle\Entity\PasswordRecovery') ->findAll(array('token' => $currentToken));

谢谢!

1 个答案:

答案 0 :(得分:0)

这是最终为我工作的解决方案,希望这可以提供帮助。如果有更好的方法可以做我在这里发布的内容,我可以接受建议。

public function resetPasswordAction(Request $request) {

    $form = $this->createFormBuilder()

    ->add('password', 'password')

    ->add('save', 'submit', ['label' => 'Send'])

    ->getForm();

    if ($request->isMethod('POST')) {

    $form->handleRequest($request);
    $newPassword = $form->get('password')->getData();

    $currentToken = $request->query->get('token'); 

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

    $passwRecoveryObject = $em->getRepository('\UserBundle\Entity\PasswordRecovery')->findOneBy(array('token' => $currentToken));

    if (empty($passwRecoveryObject)) {   

        return new Response ("Impossible to execute the request!");
    }

    else {

    if (new \DateTime('now') > $passwRecoveryObject->getExpireDate()) 
    {

        return new Response ("Token expired!");
    }

    $idUser = $passwRecoveryObject->getIdUser()->getId(); 


    $userRecoverPassw = $em->getRepository('\UserBundle\Entity\PasswordRecovery')->findBy(array('idUser' => $idUser)); 

    $end = end($userRecoverPassw);  //$end is the last record of the password_recovery table
    $count = count($userRecoverPassw); //count of the rows in password_recovery with the id of the current User


    foreach ($userRecoverPassw as $element) { //here i'm setting to 1 the isExpired column for all the rows with the same id, except for the last one

        if (--$count <= 0) {
        break; }

        $element->setIsExpired(1);
        $em->persist($element);
        $em->flush(); 

    }

    $userResetPassw = $em->getRepository('\UserBundle\Entity\User')->findOneBy(array('id' => $end->getIdUser()->getId()));  //User that need to reset the password

    if ($end->getIsExpired()==0) {

    $userResetPassw->setPassword($newPassword);
    $end->setIsExpired(1);

    $em->persist($userResetPassw);
    $em->flush(); 

    $em->persist($end);
    $em->flush(); 

    return new Response ("Password succesfully updated!");
    }

    else { return new Response ("Expired link!");}

} }



    return $this->render('UserBundle:AccountUser:reset_password.html.php', array(
        'form' => $form->createView(),));


}