与许多人的关系,没有这个


Relation to many and get without this

User:
id | name
1  | one
2  | two
3  | three
4  | four
5  | five

House:
id | name
1  | London
2  | Barcelona
UserHouse:
id_user | id_house
 1      |  1
 2      |  2
 4      |  1

$q = Doctrine_Query::create()
  ->from('User u')
   // many selectors here
  ->leftJoin('u.UserHouse uh')
  ->addWhere('????????');
$users = $q->execute();

我想在没有 House 的情况下获取所有用户(即 - 不在表中 UserHouse( - 这应该返回我用户 3 和 5。

我知道,我可以使用:

->from('UserHouse uh')

和下一个与用户的关系,但我必须拥有和使用:

  ->from('User u')

因为我将此查询与许多选择器一起使用 - 无法编辑它。我必须从 ->from('User u'( 开始

那么我必须填写什么 ->addWhere('????????'( 这让我没有房子的用户?

如果没有教义,我怎么能用简单的SQL来获得这个?

在SQL中,这种类型的查询需要所谓的EXCEPTION JOIN。 一些 RDBMS 实际上将其实现为单独的类型(例如 DB2(,而其他 RDBMS 则需要使用解决方法。 在您的情况下,它相当于(在 SQL 中(:

SELECT User.* 
FROM User
LEFT JOIN UserHouse
ON UserHouse.id_user = User.id
WHERE UserHouse.id_user IS NULL

这将产生预期的"不在房子里"记录。

本网站的许多地方都有类似的例子。

我从来没有使用过教义,所以我不能在那里帮助你。 但我最好的猜测是这样的:

addWhere('uh IS NULL')

addWhere('uh.id_user IS NULL')