Advertisement
stixlink

Untitled

Nov 25th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1.   public function profileEditAction(Request $request) {
  2.  
  3.         $user = $this->getUser();
  4.         if (!is_object($user) || !$user instanceof UserInterface) {
  5.             $this->redirectToRoute('fos_user_security_login');
  6.         }
  7.  
  8.         $profile = $user->getProfile();
  9.         if (!($profile instanceof Profile)) {
  10.             $profile = new Profile();
  11.             $profile->setUser($user);
  12.         }
  13.         $em = $this->getDoctrine()->getManager();
  14.         $originalPhone = new ArrayCollection();
  15.  
  16.         foreach ($profile->getPhonenumbers() as $phone) {
  17.             $originalPhone->add($phone);
  18.         }
  19.  
  20.         $form = $this->createForm(ProfileEditType::class, $profile);
  21.  
  22.         $form->handleRequest($request);
  23.  
  24.         if ($form->isValid()) {
  25.  
  26.             $profile = $form->getData();
  27.  
  28.             foreach ($originalPhone as $phone) {
  29.                 if (false === $profile->getPhonenumbers()->contains($phone)) {
  30.                     $phone->getProfile()->removeElement($phone);
  31.                     $em->persist($phone);
  32.                 }
  33.             }
  34.  
  35.             $em->persist($profile);
  36.  
  37.             $em->flush();
  38.  
  39.             $url = $this->generateUrl('fos_user_profile_show');
  40.             $response = new RedirectResponse($url);
  41.  
  42.             return $response;
  43.         }
  44.  
  45.  
  46.         return $this->render(
  47.             '@App/Profile/profile-edit.html.twig',
  48.             ['form' => $form->createView()]
  49.         );
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement