如何在CakePHP 3中使用postGIS

我们正在运行带有Postgres数据库的CakePHP 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),4326)::geography, ' . $maxMetersAway . ')'])
        ->toArray();

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

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...