如何在 CakePHP 3 中使用 postGIS

How to use postGIS with CakePHP 3

我们 运行 CakePHP v3.x 使用 Postgres 数据库。我需要 select 一些纬度和经度与另一点的距离在 X 以内的记录。 PostGIS has a function that does just this 但似乎我必须纠正原始 SQL 查询才能使用它。

我不是在寻求编写原始查询的帮助,但我正在寻求确认原始查询方法是否是在充分利用框架的同时使用此扩展的正确方法。我搜索过,但没有找到任何库来扩展 CakePHP ORM 以包含它。或许还有第三种我没想到的选择。

[注意:这不起作用...]

public function fetch()
{
    $maxMetersAway = 10 * 1000;
    $lat = $this->request->query['lat'];
    $lng = $this->request->query['lng'];

    $stops = $this->Stops->find('all')
        ->where("ST_DWithin( POINT($lng,$lat), POINT(Stops.lng,Stops.lat), $maxMetersAway")
        ->toArray();

    $this->set(['stops'=>$stops, '_serialize' => true]);
}

我得到了 CakePHP 查询:

$stops = $this->Stops->find('all')
        ->where(['ST_DWithin( ST_SetSRID(ST_MakePoint(' . $longitude . ', ' . $latitude . '),4326)::geography, ST_SetSRID(ST_MakePoint(Stops.longitude, Stops.latitude),4326)::geography, ' . $maxMetersAway . ')'])
        ->toArray();

已更新:原版实际上无法正确使用 $maxMetersAway