我正在尝试将以下SQL字符串转换为Cake查找查询。 这样做有简单的方法吗?
SELECT longitude, latitude, firstname, surname, profile_pic, facebook_id, distance
FROM
(SELECT longitude, latitude, firstname, surname, profile_pic, facebook_id, 6371 * ACos( Cos( RADIANS( users.latitude ) ) * Cos( RADIANS( $latitude ) ) * Cos( RADIANS( $longitude ) - RADIANS( users.longitude ) ) + Sin( RADIANS( users.latitude ) ) * Sin( RADIANS( $latitude ) ) ) AS distance
FROM users
) as users
WHERE distance <=2 AND facebook_id != $facebook
ORDER BY distance
LIMIT 20
答案 0 :(得分:1)
我的代码片段做了类似的事情:
$GeoCache->virtualFields = array(
'distance' => "SQRT(POW(UserGeolocationCache.latitude - {$latitude},2) + POW(UserGeolocationCache.longitude - {$longitude},2))"
);
$users = $GeoCache->find('all', array(
'conditions' => array('distance <' => '0.05'),
'order' => array('distance asc'),
'contain' => array('User'),
));
TL; DR只是将您的距离计算填入虚拟区域并搜索它。