How to implement distance queries in CakePHP?

做~自己de王妃 提交于 2019-12-25 01:36:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!