我想选择不属于特定服务的成员.我有 3 张桌子:
I'd like to select members who are not in specific service. I have 3 tables :
成员
服务
membre_service
(membre
和 service
之间的关系)membre
service
membre_service
(relation between membre
and service
)我使用的是学说 2,在 SQL 中我的查询是:
I'm using doctrine 2 and in SQL my query is :
SELECT m.* FROM membre m WHERE m.`id` NOT IN (
SELECT ms.membre_id FROM membre_service ms WHERE ms.service_id != 29
)
在 Doctrine 中,我这样做:
In Doctrine, I do :
$qb = $this->_em->createQueryBuilder();
$qb2 = $qb;
$qb2->select('m.id')
->from('CustomEntityMembreService', 'ms')
->leftJoin('ms.membre', 'm')
->where('ms.id != ?1')
->setParameter(1, $service);
$qb = $this->_em->createQueryBuilder();
$qb->select('m')
->from('CustomEntityMembre', 'm')
->where($qb->expr()->notIn('m.id', $qb2->getDQL())
);
$query = $qb->getQuery();
//$query->useResultCache(true, 1200, __FUNCTION__);
return $query->getResult();
我收到以下错误:
语义错误] line 0, col 123 near 'm WHERE ms.id': Error: 'm' is already defined.
Semantical Error] line 0, col 123 near 'm WHERE ms.id': Error: 'm' is already defined.
同一个查询中不能定义2次相同的别名
The same alias cannot be defined 2 times in the same query
$qb = $this->_em->createQueryBuilder();
$qb2 = $qb;
$qb2->select('m.id')
->from('CustomEntityMembreService', 'ms')
->leftJoin('ms.membre', 'm')
->where('ms.id != ?1');
$qb = $this->_em->createQueryBuilder();
$qb->select('mm')
->from('CustomEntityMembre', 'mm')
->where($qb->expr()->notIn('mm.id', $qb2->getDQL())
);
$qb->setParameter(1, $service);
$query = $qb->getQuery();
return $query->getResult();
理想情况下,您应该为实体使用多对多关系,在这种情况下,您的查询会简单得多.
Ideally you should use many-to-many relation for your entity, in this case your query is going to be much simpler.
这篇关于Doctin2 中的子查询 notIN 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!