如何在CakePHP 3中使用postGIS

我们正在运行带有Postgres数据库的Cake PHP v3.x.我需要选择一些纬度和经度与另一个点相距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),' . $maxMetersAway . ')'])
        ->toArray();

更新:原始版本实际上没有正确使用$maxMetersAway.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...