结合两个不同的 DQL
Combine two different DQL
我正在尝试修改 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 中检索两个结果的建议?
感谢@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();
}
我正在尝试修改 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 中检索两个结果的建议?
感谢@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();
}