2
我使用PostgreSQL中的earthdistance模塊的各種函數,其中之一是ll_to_earth
。鑑於緯度/經度組合,我試圖通過CakePHP 1.2.5 Stable從我的數據庫中檢索最近的點。CakePHP將WHERE子句中的函數名加上引號
// set dummy data
$latitude = 30.4393696;
$longitude = -97.6200043;
// create a simple bounding box in the WHERE conditions, sort the points by location, and retrieve the nearest point
$location = $this->Location->find('first', array(
'fields' => array(
'id',
'coords',
"earth_distance(coords, ll_to_earth($latitude, $longitude)) AS distance",
),
'conditions' => array(
'coords >=' => 'll_to_earth(' . ($latitude - 0.5) . ', ' . ($longitude - 0.5) . ')',
'coords <=' => 'll_to_earth(' . ($latitude + 0.5) . ', ' . ($longitude + 0.5) . ')',
),
'order' => array('distance ASC'),
));
的SQL輸出(格式爲便於閱讀的緣故)結束這樣看:
SELECT
"Location"."id" AS "Location__id",
"Location"."coords" AS "Location__coords",
earth_distance(coords, ll_to_earth(30.4393696, -97.6200043)) AS distance FROM "locations" AS "Location"
WHERE
"coords" >= 'll_to_earth(29.9393696, -97.1200043)' AND
"coords" <= 'll_to_earth(30.9393696, -96.1200043)'
ORDER BY
"distance" ASC
LIMIT 1
你能看到我的問題在於:CakePHP會引用我發現條件,包括功能名稱ll_to_earth
。我只需要通過模型的query
方法手動查詢,或者有沒有辦法告訴Cake,ll_to_earth
是一個DB函數,而不是引用它?
* facepalm *哇,自從我必須這樣做以來,我忘了我可以這麼做了。謝謝! – 2009-10-16 17:29:13