Php 如何在symfony原则中编写子查询

Php 如何在symfony原则中编写子查询,php,symfony,doctrine-orm,doctrine,api-platform.com,Php,Symfony,Doctrine Orm,Doctrine,Api Platform.com,我正在做一个symfony项目,后端是在doctrine&api平台框架中开发的。我需要获取一些详细信息,同时检查另一个表中的一个字段,这将是一个状态。我们使用这个状态来处理前端的某些事情 我试过:- $qb = $this->createQueryBuilder('contact'); $qb2=$qb; $sub_query = $qb2->select('field') ->from('OtherTable','g') ->where("'id= '

我正在做一个symfony项目,后端是在doctrine&api平台框架中开发的。我需要获取一些详细信息,同时检查另一个表中的一个字段,这将是一个状态。我们使用这个状态来处理前端的某些事情

我试过:-

$qb = $this->createQueryBuilder('contact');
$qb2=$qb;
$sub_query = $qb2->select('field')
    ->from('OtherTable','g')
    ->where("'id= '".$personId."'")
    ->OrderBy('updated_at', 'DESC')
    ->setMaxResults(1)
    ->getQuery()
    ->getResult();

$qb->select("contact.id, 
                contact.title, 
                count (distinct person.id) as 
number_of_contacts_with_email',(".$sub_query.") as status")
 ->leftjoin('contact.people', 'person')
 ->leftJoin('person.jobs', 'jobs')
 ->groupBy('contact.id, contact.title');


$query=$qb->getQuery();
$result = $qb->getQuery()->getArrayResult();
return $result;
我在执行查询时遇到此错误

  [Semantical Error] line 0, col 59 near 'OtherTable g': Error: Class 'OtherTable' is not defined.

如何在此处编写此子查询?是否有解决方案?

您只需使用子查询的DQL,例如:

// Don't take the query/result instances
$sub_query = $qb2->select('field')
    ->from('OtherTable','g')
    ->where("'id= '".$personId."'")
    ->OrderBy('updated_at', 'DESC')
    ->setMaxResults(1);
和使用

$qb->select("contact.id, 
                contact.title, 
                count (distinct person.id) as 
number_of_contacts_with_email',(".$sub_query->getDQL().") as status")
 ->leftjoin('contact.people', 'person')
 ->leftJoin('person.jobs', 'jobs')
 ->groupBy('contact.id, contact.title');

希望有此帮助

Hi@Matteo感谢您的回复,我尝试了您的解决方案,但仍然出现此错误:-[Semantic error]第0行,第220列靠近“OtherTable g WHERE”:错误:未定义类“OtherTable”。我发现的问题是我们无法在条令中使用限制运行子查询。是否有其他解决方案?我也在检查!因此,我认为我需要用SQL编写除DQL之外的所有查询,以满足我的需求:(如果我错了,请纠正我!可能的副本仍未解决,我收到错误!类未定义!存在
OtherTable
实体表?OtherTable实体存在,但实体未在我的查询中正确执行,我已在存储库中包含$injected The enity。如何在nativequery中编写整个查询,nativEquiry不适合我!