DIAL Symphonies 2 分享日期

DQL Symfony 2 Delete Data

我只是一个关于 Symfony2 中的 DQL 函数的问题,我有代码:

    public function editAction(Request $request, $regkey){
    try{
        if($request->isMethod('POST')){
            $newpassword = $request->request->get('newpassword');
            $confirmpassword = $request->request->get('confirmpassword');
            $username = $request->request->get('username');
            if ($newpassword != $confirmpassword){
                return new JsonResponse(['userRegistration' => 'Confirm password is wrong, please make sure your confirm password']);
            }

            $userRegistrationId = $this->getDoctrineRepo(UserRegistration::class)->getIdbyRegisterKey($regkey);
            if ($userRegistrationId == null){
                return new JsonResponse(['userRegistration' => 'Registration key is wrong, Please make sure your registration']);
            }
            $userNameCorrect = $userRegistrationId[0]['user']['username'];
            $userid = $userRegistrationId[0]['user']['id'];

            if ($username != $userNameCorrect){
                return new JsonResponse(['userRegistration' =>  'user name is wrong, please insert user name correctly']);
            }

            $userEntity = $this->getDoctrineRepo(User::class)->find($userid);
            $userEntity->setActivate(1);
            $userEntity->setActivationDate(new \DateTime());

        }
    }
    catch (Exception $ex){
        return new JsonResponse(['errMsg' => $ex], 500);
    }
    return new JsonResponse(['userRegistration' => 'Validation user success']);
}

有一个 $userRegistrationId,我想删除那个数据,我该如何编码这个问题?

如果你想删除你获取到 $user Registration If 的 User Registration ,你需要获取对象而不是 id。您可以使用 findOneBy() 并使用 EntityManager 删除该对象。