我有一个具有point(postgis)属性的实体。我需要返回按用户坐标和实体点之间的距离排序的该实体的集合。
为此,我添加了一个聚合函数来计算该距离并将其添加到ORDER BY中,但我不想返回它。我只需要实体的返回和对象数组。
没有订单,结果是:
[
{
"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距离添加下一个代码时(计算器在用户坐标和点之间)
$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但删除/隐藏距离属性吗?
截至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")