添加虚拟列以按它排序但不 return 它

Add virtual column for order by it but does not return it

我有一个实体,它有一个点 (postgis) property.I 需要 return 按用户坐标和实体点之间的距离排序的那个实体的集合。

为此,我添加了一个聚合函数来计算该距离并将其添加到 ORDER BY 中,但我不想 return 它。我只需要 return 和实体对象数组。

没有排序依据,结果是:

[
  {
    "user": "/api/users/1",
    "id": 1,
    "gender": "MALE",
    "createdAt": "2019-04-05T11:03:03+02:00",
    "updateAt": "2019-04-11T11:34:06+02:00",
    "birthdate": "1991-05-13T08:02:32+02:00",
    "deletedAt": null,
    "town": "Miami"
  },
  {
    "user": "/api/users/3",
    "id": 2,
    "gender": "MALE",
    "createdAt": "2019-04-05T13:59:30+02:00",
    "updateAt": "2019-04-11T10:57:40+02:00",
    "birthdate": "1999-04-05T11:48:46+02:00",
    "deletedAt": null,
    "town": "New York"
  },
  {
    "user": "/api/users/7",
    "id": 3,
    "gender": "MALE",
    "createdAt": "2019-04-11T11:11:03+02:00",
    "updateAt": "2019-04-11T11:11:03+02:00",
    "birthdate": "1991-05-13T08:02:32+02:00",
    "deletedAt": null,
    "town": "New York"
  }
]

当我为 ORDER BY disntace 添加下一个代码时(在用户坐标和点之间计算)

$queryBuilder
    ->addSelect("ST_Distance(o.point, ST_Point(:longitude,:latitude)) AS distance")
    ->addOrderBy("distance", "ASC")
    ->setParameter("longitude", $longitude)
    ->setParameter("latitude", $latitude)
;

我得到:

[
  {
    "0": {
        "user": "/api/users/1",
        "id": 1,
        "gender": "MALE",
        "createdAt": "2019-04-05T11:03:03+02:00",
        "updateAt": "2019-04-11T11:34:06+02:00",
        "birthdate": "1991-05-13T08:02:32+02:00",
        "deletedAt": null,
        "town": "Miami"
    },
    "distance": "106496.35623204"
  },
  {
    "0": {
        "user": "/api/users/7",
        "id": 3,
        "gender": "MALE",
        "createdAt": "2019-04-11T11:11:03+02:00",
        "updateAt": "2019-04-11T11:11:03+02:00",
        "birthdate": "1991-05-13T08:02:32+02:00",
        "deletedAt": null,
        "town": "New York"
    },
    "distance": "109073.2944295"
  },
  {
    "0": {
        "user": "/api/users/3",
        "id": 2,
        "gender": "MALE",
        "createdAt": "2019-04-05T13:59:30+02:00",
        "updateAt": "2019-04-11T10:57:40+02:00",
        "birthdate": "1999-04-05T11:48:46+02:00",
        "deletedAt": null,
        "town": "New York"
    },
    "distance": "285892.32591062"
  }
]

我需要结果看起来像第一个 json。可以添加 ORDER BY 但 remove/hide 距离 属性?

截至 doctrine 2.2, Scalar mappings can now be omitted from DQL result

使用 HIDDEN 关键字,以便从结果中省略计算字段:

->addSelect("ST_Distance(o.point, ST_Point(:longitude,:latitude)) AS HIDDEN distance")

DQL select expressions documentation

DQL examples