我有一个可以有很多朋友的用户。表示关系是自引用的。我现在需要根据性别计算我的朋友朋友的数量。到目前为止,我想出了以下解决方案。
我的用户实体:
<?php
namespace AcmeUserBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AcmeUserBundleEntityRepositoryUserRepository")
* @ORMTable(name="users")
*/
class User extends BaseUser
{
/**
* @ORMManyToMany(targetEntity="AcmeUserBundleEntityUser", inversedBy="friendsOf")
* @ORMJoinTable(name="map_user_friend",
* joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="friend_id", referencedColumnName="id")}
* )
*/
protected $friends;
/**
* @ORMManyToMany(targetEntity="AcmeUserBundleEntityUser", mappedBy="friends")
*/
private $friendsOf;
/**
* @ORMColumn(type="string", length=1, nullable=true)
*/
protected $gender;
// other fields ...
}
我的用户存储库具有以下方法:
public function getFriendsQuery(User $user)
{
return $this->getEntityManager()->createQueryBuilder()
->select('u as user, COUNT(DISTINCT mf.id) as male_friends, COUNT(DISTINCT ff.id) as female_friends')
->from('OneupUserBundleEntityUser', 'u')
->leftJoin('u.friendsOf', 'f')
->leftJoin('u.friendsOf', 'mf', 'with', 'mf.gender = :gender')->setParameter('gender', 'm')
->leftJoin('u.friendsOf', 'ff', 'with', 'ff.gender = :gender')->setParameter('gender', 'w')
->where('f = :user')->setParameter('user', $user)
->groupBy('u.id')
;
}
在控制器中,我现在首先通过返回DoctrineCommonUtilDebug::dump($qb->getQuery()->getSQL());
转储 SQL
SELECT u0_.username AS username0,
u0_.username_canonical AS username_canonical1,
-- ... --
u0_.gender AS gender19,
-- ... --
COUNT(DISTINCT u1_.id) AS sclr32,
COUNT(DISTINCT u2_.id) AS sclr33
FROM users u0_
LEFT JOIN map_user_friend m4_ ON u0_.id = m4_.friend_id
LEFT JOIN users u3_ ON u3_.id = m4_.user_id
LEFT JOIN map_user_friend m5_ ON u0_.id = m5_.friend_id
LEFT JOIN users u1_ ON u1_.id = m5_.user_id
AND (u1_.gender = ?)
LEFT JOIN map_user_friend m6_ ON u0_.id = m6_.friend_id
LEFT JOIN users u2_ ON u2_.id = m6_.user_id
AND (u2_.gender = ?)
WHERE u3_.id = ?
GROUP BY u0_.id
m
为男性,f
为女性,21
为当前用户。现在遵循奇怪的行为。 DoctrineCommonUtilDebug::dump($qb->getQuery()->execute());
回报我
array (size=2)
0 =>
array (size=3)
'user' => string 'OneupUserBundleEntityUser' (length=28)
'male_friends' => string '2' (length=1)
'female_friends' => string '2' (length=1)
1 =>
array (size=3)
'user' => string 'OneupUserBundleEntityUser' (length=28)
'male_friends' => string '0' (length=1)
'female_friends' => string '0' (length=1)
如果我直接在 mysql 中执行查询,我会得到另一个计数:
+--------------+-----------------+-----------------+
| username0 | sclr32 (male) | sclr33 (female) |
+--------------+-----------------+-----------------+
| foobar12 | 1 | 2 |
+--------------+-----------------+-----------------+
| foobar2 | 2 | 0 |
+--------------+-----------------+-----------------+
这是正确的。这是一个教义错误,还是我只是做错了?
为了回答我自己的问题,错误出在存储库中的参数上
->leftJoin('u.friendsOf', 'mf', 'with', 'mf.gender = :gender')->setParameter('gender', 'm')
->leftJoin('u.friendsOf', 'ff', 'with', 'ff.gender = :gender')->setParameter('gender', 'w')
错了。学说对这两个条目都采用第二个值。
->leftJoin('u.friendsOf', 'mf', 'with', 'mf.gender = :male')->setParameter('male', 'm')
->leftJoin('u.friendsOf', 'ff', 'with', 'ff.gender = :female')->setParameter('female', 'w')