0

次のようなクエリがあります(Googleマップから):

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

私はこれを Cakephp のカスタム検索にしたいのですが、どうすればいいですか?

MY_LAT と MY_LONG は渡されるパラメーターです。LATITUDE と LONGITUDE は zips テーブルの列です。

4

1 に答える 1

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.

于 2012-07-18T10:10:38.687 に答える