Search code examples
symfonydql

DQL Symfony 2 Delete Data


I just a problem about DQL Function in Symfony2, i have code :

    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']);
}

there is a $userRegistrationId, i want to delete that data, how can i code this problem ?


Solution

  • If you want to delete UserRegistration where you fetched into $userRegistrationId , you need to get object not id. You can use findOneBy() and use EntityManager for delete that object.