php - 如何在 CakePHP 3 中使用 postGIS

标签 php postgresql cakephp postgis

我们使用 Postgres 数据库运行 CakePHP v3.x。我需要选择一些经纬度与另一点在 X 距离以内的记录。邮政地理信息系统 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

关于php - 如何在 CakePHP 3 中使用 postGIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34298777/

相关文章:

php - YII2平面阵列

php - 来自外部 php 文件的 CRUD

sql - Postgres UPSERT UPdate 使用行值

php - 如何在结帐时更改 WooCommerce 文本运送

php - 表输出 php 和 mysql 没有得到结果

java - Hibernate、Spring、PostgreSQL - 列索引超出范围

postgresql - Dockerized Spring Boot + Postgres连接被拒绝

php - cakephp 从数据库中获取数据

cakephp - 在 cakePHP 中将组件变量传递给 Controller

php - 哪个框架使用 : CodeIgniter, Symfony 或 CakePHP?