我有一组完成某项测试的用户和一份获得该测试邀请的用户列表。现在,我想获取没有完成测试的所有用户。我认为对两个集合(如数组)进行差异化很简单,但只有Doctrine_Collection::merge()
是可能的。
我的数据模型(为清晰起见,很多内容):
Invite:
columns:
id: integer (10)
relations:
users:
foreignAlias: invites
class: User
refClass: UserInvite
UserInvite:
columns:
user_id: integer (10)
invite_id: integer (10)
relations:
user:
class: User
foreignAlias: userInvite
invite:
class: Invite
foreignAlias: userInvite
Test:
columns:
id: integer (10)
user_id: integer (10)
invite_id: integer (10)
relations:
user:
class: User
foreignAlias: tests
invite:
class: Invite
foreignAlias: tests
现在我可以使用这两个系列:
$invite = new Invite;
$invite = $invite->users; // All the users who got an invite
$invite = $invite->tests; // All the tests performed for this invite
获得所有用户的最佳方法是什么?我可以执行SQL查询,但我不喜欢在OOP php或DQL查询中执行此操作。在SQL中,我可以这样做:
SELECT u.name, u.id
FROM user u
LEFT JOIN userinvite i
ON i.user_id = u.id
LEFT JOIN test t
ON t.user_id = u.id
WHERE i.id IS NOT NULL
AND t.id IS NULL
答案 0 :(得分:0)
好的,我现在知道我必须通过User模型本身解决这个问题。此DQL查询从特定邀请中提取所有错过测试的用户:
$user = new User;
$users = $user->getTable()
->getQueryObject()
->leftJoin('User.userInvite i')
->leftJoin('User.tests t')
->where('i.invite_id = ?', $invite->id)
->andWhere('i.user_id IS NOT NULL')
->andWhere('t.user_id IS NULL')
->execute();