DQL Queries not returning what I would like

I use two DQL queries. On my website I have users and Formations. I have defined a ManyToMany relation on User like this:

/**
     * Many User have Many Phonenumbers.
     * @ManyToMany(targetEntity="Formation")
     * @JoinTable(name="users_favorites",
     *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="formation_id", referencedColumnName="id", unique=true)}
     *      )
     */
    private $favorites;

Users can add Formations to their favorite.

The first query, I would like to get all the Formations but also, the users that added this formation to their favorite.

    public function findAllWithFormation(){
        return $this->createQueryBuilder('user')
            ->join('user.favorites', 'formation')
            ->addSelect('formation')
            ->addSelect('user')
            ->getQuery()
            ->getOneOrNullResult()
            ;
    }

This return nothing, not a single formation. And then I have this one, I want to get all the formation that the User has added to their favorite:

public function findAllByUser(int $idUser){
        return $this->createQueryBuilder('user')
            ->leftJoin('user.favorites', 'formation')
            ->addSelect('formation')
            ->addSelect('user')
            ->andWhere('user.id = :id')
            ->setParameter('id', $idUser)
            ->getQuery()
            ->getOneOrNullResult()
            ;
    }

And same thing, this returns nothing.

The thing is, I can’t var_dump($result) because it makes my website crash :/ I’m really stuck because I can’t even see what I get, if I get anything.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *