问题
I have a query (from Google Maps) like so:
SELECT ( 3959 * acos( cos( radians(MY_LAT) ) * cos( radians( LATITUDE ) ) *
cos( radians( LONGITUDE ) - radians(MY_LONG) ) + sin( radians(MY_LAT) ) * sin( radians(
LATITUDE ) ) ) ) AS distance
FROM zips
ORDER BY distance
I would rather this be a custom find in Cakephp, so how do I do that?
MY_LAT and MY_LONG are params passed in. LATITUDE and LONGITUDE are columns in zips table.
回答1:
You can use a behavior like the geocoder behavior ( https://github.com/dereuromark/tools/blob/2.0/Model/Behavior/GeocoderBehavior.php#L208 ) and attach it to your model with
$this->Model->Behaviors->attach('Tools.Geocoder');
and call
$this->Model->setDistanceAsVirtualField($lat, $lng, $fieldName);
this way a virtual field is attached and will retrieve and contain your distance value just like any other normal field. you can also sort/filter by that field then.
来源:https://stackoverflow.com/questions/11535808/how-to-implement-distance-queries-in-cakephp