Doctrine orm 条令:主实体id位于子查询数组中的子查询

Doctrine orm 条令:主实体id位于子查询数组中的子查询,doctrine-orm,Doctrine Orm,我想搜索未分配房间的人。我提出了以下问题: public function findByWithoutRoom() { $qb = $this->getEntityManager()->createQueryBuilder(); $qb2 = $this->getEntityManager()->createQueryBuilder(); $qb ->select('p') ->from('MyPeop

我想搜索未分配房间的人。我提出了以下问题:

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

    $qb
        ->select('p')
        ->from('MyPeopleBundle:Person', 'p')

        ->where(
            $qb->expr()->exists(
                $qb2->select('r')
                    ->from('MyAccommodationBundle:Room', 'r')
                    ->andWhere($qb2->expr()->like('r.currentPeople', ':person'))
                    ->setParameter('person', '%i:'.$person_id.';%')

                    ->getDQL()
            )
        )

    $result = $qb->getQuery()->execute();
    return $result;
}
我怎么能用p.id代替个人id?注意:currentPeople属性的类型为“数组”(而不是“简单数组”)

更新:

我还尝试了以下方法:

public function finByWithoutRoom()
{
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb
        ->select('p')
        ->from('MyPeopleBundle:Person', 'p')
        ->leftJoin('MyAccommodationBundleV2:Room', 'r')
        ->andWhere($qb->expr()->like('r.currentPeople', '%i:p.id%'));

    $result = $qb->getQuery()->execute();
    return $result;
}
但是,这给了我以下错误:

[Syntax Error] line 0, col 114: Error: Expected StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression, got '%'

您可以直接使用主查询的别名,例如:

$qb
    ->select('p')
    ->from('MyPeopleBundle:Person', 'p')
    ->where(
        $qb->expr()->isNotNull(
            $qb2->select('r')
                ->from('MyAccommodationBundle:Room', 'r')
                ->andWhere($qb->expr()->like('r.currentPeople', 'p.id'))

                ->getDQL()
        )
    )

    ->setParameter('from', $from)
    ->setParameter('to', $to);
我建议使用
不存在
而不是
不为空
(我认为结果是一样的)。例如:

    $qb->andWhere($qb->expr()->not($qb->expr()->exists($qb2->getDQL())));

希望这有帮助

谢谢!我正在检查这个。我想知道是否有一个id为100的人和一个id为1000的人,它可能会返回为“NOTNULL”,而事实可能并非如此。p、 id也可能出现在数组中的其他位置。稍后将回到这里。它仅在我执行->setParameter(':myid','%i:556%')时有效,其中556是硬编码的p.id。有没有办法按照您指定的格式添加通配符?($qb->expr()->like('r.currentPeople',p.id'))我们可以使用如上所述的SQL concat,但这取决于您使用的条令版本(如果有)。