فهرست منبع

Ajout d'une requete pour le profile

François Drouhard 2 سال پیش
والد
کامیت
56dad545d5
2فایلهای تغییر یافته به همراه15 افزوده شده و 3 حذف شده
  1. 4 3
      src/Controller/ProfilController.php
  2. 11 0
      src/Repository/ProfileRepository.php

+ 4 - 3
src/Controller/ProfilController.php

@@ -2,10 +2,10 @@
 
 namespace App\Controller;
 
-use App\Entity\User;
 use Doctrine\ORM\EntityManagerInterface;
 use App\Form\UserEditProfilType;
 use App\Form\ProfileType;
+use App\Repository\ProfileRepository;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
@@ -37,9 +37,10 @@ class ProfilController extends AbstractController
     /**
      * @Route("/preferences/", name="user_preferences")
      */
-    public function mesPreferencesAction(Request $request, EntityManagerInterface $em): Response
+    public function mesPreferencesAction(Request $request, EntityManagerInterface $em, ProfileRepository $profileRepo): Response
     {
-        $profile = $this->getUser()->getProfile();
+        $profile = $profileRepo->findByUser($this->getUser());
+
         $form = $this->createForm(ProfileType::class , $profile);
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())

+ 11 - 0
src/Repository/ProfileRepository.php

@@ -3,6 +3,7 @@
 namespace App\Repository;
 
 use App\Entity\Profile;
+use App\Entity\User;
 use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
 use Doctrine\Persistence\ManagerRegistry;
 
@@ -19,6 +20,16 @@ class ProfileRepository extends ServiceEntityRepository
         parent::__construct($registry, Profile::class);
     }
 
+    public function findByUser(User $user): ?Profile
+    {
+        return $this->createQueryBuilder('p')
+            ->andWhere('p.user = :user')
+            ->setParameter('user', $user->getId())
+            ->getQuery()
+            ->getOneOrNullResult()
+        ;
+    }
+
     // /**
     //  * @return Profile[] Returns an array of Profile objects
     //  */