我有两个阶段的查询:
public function findOtherRosters($user){
$q2 = $this->createQueryBuilder('r')
->leftJoin('r.members', 'm')
->addSelect('m')
->where('m.user = :user')
->setParameter('user', $user);
$qb = $this->createQueryBuilder('ro');
$qb
->where('ro not IN (:rosters)')
->setParameter('rosters', $q2->getQuery()->getResult())
;
return $qb->getQuery()
->getResult();
我需要检查第一个查询($ qb)是否返回结果。如果结果我运行查询,如果不是另一个查询。目前我的代码如下:
public function findOtherRosters($user){
$q2 = $this->createQueryBuilder('r')
->leftJoin('r.members', 'm')
->addSelect('m')
->where('m.user = :user')
->setParameter('user', $user);
if ( is_null($q2) ){ // the issue is here
$qb = $this->createQueryBuilder('ro');
$qb
->where ('ro not IN (:rosters)')
->setParameter('rosters', $q2->getQuery()->getResult())
;
} else {
$qb = $this->createQueryBuilder('ro');
}
return $qb->getQuery()
->getResult();
}
问题在于is_null;我需要检查第一个查询是否返回任何行,如果为null则返回。 有什么想法吗?
答案 0 :(得分:2)
我可能在这里误解了你的意图但是你不应该getResult()
先检查一下吗?
这样的事情?
public function findOtherRosters($user){
$q2 = $this->createQueryBuilder('r')
->leftJoin('r.members', 'm')
->addSelect('m')
->where('m.user = :user')
->setParameter('user', $user);
$q2Result = $q2->getQuery()->getResult(); // <<---- THIS
if ( count($q2Result) ){ // the issue is here <<--- THIS
$qb = $this->createQueryBuilder('ro');
$qb
->where ('ro not IN (:rosters)')
->setParameter('rosters', $q2Result) // <<---- THIS
;
} else {
$qb = $this->createQueryBuilder('ro');
}
return $qb->getQuery()
->getResult();
}