2009-10-16 6 views
2

PostgreSQL의 earthdistance 모듈에있는 다양한 함수 중 하나 인 ll_to_earth을 사용하고 있습니다. 위도/경도 조합을 감안할 때 CakePHP 1.2.5 Stable을 통해 데이터베이스에서 가장 가까운 지점을 검색하려고합니다.CakePHP WHERE 절에서 함수 이름을 따옴표로 묶습니다.

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의 함수를 포함하여, 나의 찾기 조건을 인용한다

// 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 출력은 다음과 같이 찾고 끝 이름은 ll_to_earth입니다. 모델의 query 메소드를 통해 수동으로 쿼리 할 필요가 있을까요, 아니면 ll_to_earth이 DB 함수이고이를 인용하지 않기를 케이크에 알릴 수있는 방법이 있습니까?

답변

3

이처럼 할 수 있어야 :

$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) . ') 
     and coords <= ll_to_earth(' . ($latitude + 0.5) . ', ' . ($longitude + 0.5) . ')'), 
    'order' => array('distance ASC'), 
)); 

그러나 당신이 그의 자신을 알아서해야합니다 그래서 또한 케이크 입력으로 수행되는 모든 소독을 무시합니다.

+0

* facepalm * 와우, 할 수있는 것을 잊어 버렸기 때문에 오랜 시간이 걸렸습니다. 감사! –

관련 문제