How to use postGIS with CakePHP 3

We run CakePHP v3.x with the Postgres database. I need to select some records whose latitude and longitude are at distance X from another point. PostGIS has a function that does just that , but it seems that I have to fix the original SQL queries in order to use it.

I am not asking for help in writing a raw query, but I'm looking for confirmation as to whether the raw query approach is the right way to use this extension, making the best use of the structure. I searched and found no libraries for the CakePHP ORM extension to enable this. Perhaps there is a third option that I did not think about.

[NOTE: This does not work ...]

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]);
}
+4
1

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.

+1

Source: https://habr.com/ru/post/1620193/


All Articles