1

I want to convert this query to cakephp

SELECT l.* , (
                (
                (
                ACOS( SIN( (
                $lat * PI( ) /180 ) ) * SIN( (
                l.lat * PI( ) /180 )
                ) + COS( (
                $lat * PI( ) /180 )
                ) * COS( (
                l.lat * PI( ) /180 )
                ) * COS( (
                ( $lng  -  l.long ) * PI( ) /180 )
                )
                )
                ) *180 / PI( )
                ) *60 * 1.1515 * 1.609344
                ) AS  `distance` 
                FROM  hosts l 
                HAVING  `distance` <= 50
                ORDER BY  `distance` ASC

Help me please ..... It would be more helpfull if I can add that in contiions array like

$this->paginate = array('conditions' => $conditions , 
                                        'order' => array('Host.distance' => 'ASC')
                                    );
4

1 回答 1

0

您可以尝试在调用之前重新定义虚拟字段Model::find:

// Controller code

$this->Note->virtualFields = array(
    'distance' => "( 3959 * acos( cos( radians($lat) ) * cos( radians( Note.latitude ) ) * cos( radians( Note.longitude ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( Note.latitude ) ) ) )"
);
$data = $this->Note->find('all', array('fields' => array('distance'), 'conditions' => array('distance <' => 1)));

如果可行,您可能需要创建一个模型方法来做到这一点

你也可以用分页来做到这一点。

于 2013-08-26T11:31:28.333 回答