学说2或其中的条件

时间:2014-04-27 11:46:07

标签: doctrine-orm

我试图在我的学说查询中创建where条件;

条件应该是;

返回所有已发送邮件:

sender = A and receiver = B
     

     

B; sender = B和receiver = A

下面是我的查询;只从查询返回第一个条件。

public function getMessageHistory()
    { 
        $qb = $this->getEntityManager()->createQueryBuilder();

        $qb->select(array('u'))
            ->from('Messages\Entity\Messages','u')
            ->where  ('u.senderUserId = :senderId AND u.receiverUserId = :receiverId')
            ->orWhere('u.senderUserId = :receiverId AND u.receiverUserId = :senderId')
            ->setParameter('senderId',(int)$this->getUserId())
            ->setParameter('receiverId',(int)$this->getRecipientId());  

            $query = $qb->getQuery();

            return $data = $query->getArrayResult(); 
    }

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我会这样建立:

$qb->select(array('u'))
    ->from('Messages\Entity\Messages','u')
    ->where(
        $qb->expr()->orX(
            $qb->expr()->andX(
                $qb->expr()->eq( 'u.senderUserId', ':senderId' ),
                $qb->expr()->eq( 'u.receiverUserId', ':receiverId' )
            ),
            $qb->expr()->andX(
                $qb->expr()->eq( 'u.senderUserId', ':receiverId' ),
                $qb->expr()->eq( 'u.receiverUserId', ':senderId' )
            ),
        )
    )
    ->setParameter('senderId',(int)$this->getUserId())
    ->setParameter('receiverId',(int)$this->getRecipientId());