Filter 条令查询:从关联中选择

Filter 条令查询:从关联中选择,filter,doctrine,associations,dql,Filter,Doctrine,Associations,Dql,我试图为以下情况创建查询,但首先是使用的实体: 用户: 友谊: /** * The user who started the friend request * * @var \Entity\User * @ManyToOne(targetEntity="User", inversedBy="friendshipsFrom") */ protected $from = null; /** * The user who got the friend request * * @v

我试图为以下情况创建查询,但首先是使用的实体:

用户:

友谊:

/**
 * The user who started the friend request
 * 
 * @var \Entity\User
 * @ManyToOne(targetEntity="User", inversedBy="friendshipsFrom")
 */
protected $from = null;

/**
 * The user who got the friend request
 * 
 * @var \Entity\User
 * @ManyToOne(targetEntity="User", inversedBy="friendshipsTo")
 */
protected $to = null;
我希望这能很好地解释这一点。如果我添加一个朋友,将创建一个新的友谊实体,其中我作为“from”,另一个用户作为“to”

所以现在我想选择一个用户的所有朋友。我已经知道了,下面是我的解决方案:

$query = $qb->select('u, ff, ft')
            ->from('KontakteApp\Api\Entity\User', 'u')
            ->leftJoin('u.friendshipsTo', 'ft')
            ->leftJoin('u.friendshipsFrom', 'ff')
            ->where('ft.from = :user')
            ->orWhere('ff.to = :user')
            ->andWhere('u.id != :user')
            ->getQuery();
那么现在的问题是什么?我意识到,在这种情况下,如果我找到id为2的用户的朋友(这里id=3),则

 {
    "id": 3,
    "friendshipFrom": [
        {
            "id": 42,
            "from": 3,
            "to": 2,
            "status": "open"
        },
       **HERE ARE NO MORE FRIENDSHIPS**
    ],
    "friendshipTo": [
        This is array is full of friendships of this user
    ]
}
FriendsFrom中没有更多的友谊,除了在my select中显示用户的友谊。这是因为选择('u,ff,ft')。 现在我希望FriendsTo也发生同样的情况,所以在这种情况下,朋友的FriendsTo应该是空的

总结:如何筛选关联实体

 {
    "id": 3,
    "friendshipFrom": [
        {
            "id": 42,
            "from": 3,
            "to": 2,
            "status": "open"
        },
       **HERE ARE NO MORE FRIENDSHIPS**
    ],
    "friendshipTo": [
        This is array is full of friendships of this user
    ]
}