许多用户实体在注释中使用联合表有许多房屋实体
class User
{
/**
* @ORM\ManyToMany(targetEntity="House")
* @ORM\JoinTable(name="house_user",
* joinColumns={@ORM\JoinColumn(name="user_uuid", rerencedColumnName="uuid") },
* inverseJoinColumns={@ORM\JoinColumn(name="house_uuid", ferencedColumnName="uuid") }
* }
* )
*/
public $houses;
我需要从给定用户居住的所有房屋中获取所有邻居,包括用户本身。我还需要得到这个邻居的反对。
使用SQL,我可以通过联接联合表2次来获得此信息:
select DISTINCT usr.*
FROM users usr
LEFT JOIN house_user houseDUser ON houseDUser.user_uuid = 'uuid-v4-goes-here'
LEFT JOIN house h on houseDUser.housing_complex_uuid = h.uuid
LEFT JOIN house_user houseUser ON houseUser.housing_complex_uuid = h.uuid
WHERE usr.uuid = houseUser.user_uuid
所以我可以使用QueryBuilder与邻居取得联系(但我认为不是那么优雅):
$qb = $this->reateQueryBuilder('usr')
->addSelect('users')
->leftJoin(HouseUser::class, 'houses', Join::WITH, 'usr MEMBER OF houses.users')
->leftJoin(User::class, 'users', Join::WITH, 'users MEMBER OF houses.users')
->where('usr = :userUuid')
->setParameter('userUuid', (string)$user->getUuid());
$paginator = new Paginator($qb);
$count = count($paginator);
但是,我每次都得到1分。是否有正确的方法来执行这样的查询?