2015-12-15 42 views
4

我們使用Postgres數據庫運行CakePHP v3.x。我需要選擇一些經緯度在距離另一點X距離範圍內的記錄。 PostGIS has a function that does just this,但似乎我必須正確的原始SQL查詢才能使用它。如何在CakePHP中使用postGIS 3

我不是在尋求幫助來編寫原始查詢,但我正在尋求確認原始查詢方法是否是在充分利用框架的同時使用此擴展的正確方法。我搜索了並沒有發現任何庫來擴展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]); 
} 

回答

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正確。