我正在尝试修改DQL以组合两个不同的结果:
class ContentRepository extends EntityRepository
{
/**
* @param string $userId
* @return array
*/
public function findOwnerReadByUserId(string $userId): array
{
$qb = $this->createQueryBuilder('c');
$qb->select('c')
->innerJoin('c.reactions', 'rea', Join::WITH, $qb->expr()->eq('rea.content', 'c.id'))
->where('c.userId = :userId')
->orderBy('rea.createdAt', 'DESC')
->setParameters(['userId' => $userId]);
return $qb->getQuery()->getResult();
}
/**
* @param string $userId
* @return array
*/
public function findOtherReadByUserId(string $userId): array
{
$qb = $this->createQueryBuilder('c');
$qb->select('c')
->innerJoin('c.receivers', 'rec', Join::WITH, $qb->expr()->eq('rec.userId', ':userId'))
->innerJoin('c.reactions', 'rea', Join::WITH, $qb->expr()->eq('rea.content', 'c.id'))
->where('rec.read = :read')
->orderBy('rea.createdAt', 'DESC')
->setParameters(['userId' => $userId, 'read' => true]);
return $qb->getQuery()->getResult();
}
}
这两个查询都像魅力一样工作但我想避免使用array_merge因为顺序而来。检索这两个结果的任何建议都会产生一个DQL?
答案 0 :(得分:1)
感谢@AlexBlex
这就是答案:
/**
* @param string $userId
* @return array
*/
public function findNotPendingByUserId(string $userId): array
{
$dql = <<<DQL
SELECT DISTINCT c
FROM ApiBundle:Content c
INNER JOIN c.receivers rec
INNER JOIN c.reactions rea
WHERE (rec.read = true AND (c.userId = :userId OR rec.userId = :userId))
ORDER BY rea.createdAt DESC
DQL;
return $this->getEntityManager()
->createQuery($dql)
->setParameters(['userId' => $userId])
->getResult();
}