Search code examples
symfonysymfony4symfony5

retrieve a user's favorites symfony


I need to retrieve the last 9 favorite posts of a user. I have a user profile page that lists all the publications, and all the recipes created by each user, I have a sidebar in which I want to show the last 9 favorites Favorite look here FavoriteRepository

 public function lastXFavorite(Publication $publication, User $user, $nombre)
    {
        return $this->createQueryBuilder('f')
            ->join('f.publication', 'pub')
            ->join('f.user', 'user')
            ->andWhere('pub.id = :pubId AND user.id = :userId')
            ->setParameter('pubId', $publication->getId())
            ->setParameter('userId', $user->getId())
            ->setMaxResults($nombre)
            ->getQuery()
            ->getResult();
            
    }

this is my controller for the user profile page ProfilController

/**
     * @IsGranted("ROLE_USER")
     * @Route("/profil/{id}", name="profil", methods={"GET","POST"})
     * 
     */
    public function index(User $user, PublicationManager $publicationManager): Response
    {
        // historique des les publications
        $em = $this->getDoctrine()->getManager();
        $publications = $em->getRepository('App:Publication')->findBy(
            array('users' => $user->getId()),
            array('created_at' => 'Desc')
        );

        // Lister all mes Favorites
        $favorites = $em->getRepository('App:Favorite')->findBy(
            array('user' => $user->getId())
           
        ); 

        // last 9 favorites
        
        
        

        return $this->render('profil/index.html.twig', [

            'publications' => $publications,
            'mesRecettes' => $publicationManager->getRecetteByUser($user),
            'mesFavoris' => $favorites,
            'recettes' => $publicationManager->allRecette(),
            'user' => $user,
            'lastRecettes' => $publicationManager->lastXRecette(),
            'lastPRecettes' => $publicationManager->lastPRecette($user, 6),
               

        ]);
    }

this is the manager of the publications PublicationManager

class PublicationManager
{
    protected $em;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->em = $entityManager;
    }

    public function getPublicationByUser(User $user)
    {
        return $this->em->getRepository(Publication::class)->getPublicationUser($user);
    }

    public function getRecetteByUser(User $user)
    {
        return $this->em->getRepository(Publication::class)->findBy(
            ['type' => true, 'users' => $user->getId()],
            ['created_at' => 'desc']
        );
    }

    public function allRecette()
    {
        return $this->em->getRepository(Publication::class)->findBy(
            ['type' => true],
            ['created_at' => 'desc']
        );
    }

    public function allPublication()
    {
        return $this->em->getRepository(Publication::class)->findBy([], ['created_at' => 'desc']);
    }

    public function lastXRecette()
    {
        return $this->em->getRepository(Publication::class)->lastXRecette(4);
    }

    public function lastPRecette(User $user, $cant)
    {
        return $this->em->getRepository(Publication::class)->lastPRecette($user, $cant);
    }

I am a beginner, I appreciate the information. Thank you


Solution

  • I think you shouldn't make where pub.id = :pubId.

    So try with first result and max result and order by also:

     public function lastXFavorite(Publication $publication, User $user, $nombre)
    {
        return $this->createQueryBuilder('f')
            ->join('f.publication', 'pub')
            ->join('f.user', 'user')
            ->andWhere('user.id = :userId')
            ->setParameter('userId', $user->getId())
            ->orderBy('f.id', 'DESC')
            ->setFirstResult(0)
            ->setMaxResults(9)
            ->getQuery()
            ->getResult();
            
    }