doctrine FindBy method with 'OR condition'?

后端 未结 2 1279
醉梦人生
醉梦人生 2021-01-21 00:32

Is it possible to use OR statement in Doctrine findBy() method?

I want That the output Be like this :

SELECT * FROM `f         


        
2条回答
  •  遥遥无期
    2021-01-21 00:57

    I would just use DQL... Add a function like this to your repository:

    public function findFriends($userId)
    {
        return $this->getEntityManager()
            ->createQuery('SELECT f FROM Friends WHERE userId = :userId OR FriendId = :userId')
            ->setParameter('userId', $userId)
            ->getOneOrNullResult();
    }
    

    Or use the query builder instead of DQL:

    public function findFriends($userId)
    {
        return $this->getEntityManager()
            ->createQueryBuilder()
            ->from('Friends', 'f')
            ->where('f.userId = :userId')
            ->orWhere('f.friendId = :userId')
            ->setParameter('userId', $userId)
            ->getQuery()
            ->getOneOrNullResult();
    }
    

    And then just use:

    $user = $repository->findFriends($this->getRequest()->getSession()->get('id'));
    

提交回复
热议问题