2017-08-24 67 views
0

你好,我有這個原始查詢這給了我他們的位置距離CakePHP的根據緯度和長途從數據庫中獲取結果

SELECT *,(3959 * ACOS(COS(RADIANS(42.290763)) * COS(RADIANS(location.lat)) 
    * COS(RADIANS(location.long) - RADIANS(-71.35368)) + SIN(RADIANS(42.290763)) 
    * SIN(RADIANS(location.lat)))) AS distance 
FROM `gig_post` 
JOIN user_info ON `gig_post`.user_id = user_info.user_id 
JOIN location 
ON location.`gig_post_id`=`gig_post`.gig_post_id 
ORDER BY distance; 

這裏是我的CakePHP的查詢這是越來越從多個表的結果產生。現在

public function getAllGigPosts(){ 
     $this->Behaviors->attach('Containable'); 
     return $this->find('all', array(

      'contain' => array(
       'User','UserInfo', 'GigPostAndCategory.Category','Location','GigPostAndCalender' 

      ), 
       'conditions' => array(
       'GigPost.active' => 1 

       //'OrderGigPost.request' => 0 
      ), 
      'order' => 'GigPost.gig_post_id DESC', 

      'recursive' => 0 
     )); 
    } 

在這個cakephp查詢我要實現它具有式給我的距離以及在查詢是原始查詢。我不知道如何在當前的cakephp包含查詢中插入原始查詢。

+0

** https://開頭stackoverflo w.com/questions/11535808/how-to-implement-distance-queries-in-cakephp** – ndm

回答

0

在此基礎上link

在你AppModel.php:

public function getAllGigPosts($options = array()) { 
      $defaults = array(
      'latitude' => 42.290763, 
      'longitude' => -71.35368, 
     ); 
      $options = Set::merge($defaults, $options); 

      $model_name = $Model->name; 

      $this->Behaviors->attach('Containable'); 
      return $this->find('all', array(
      'fields' => array(
       '*', 
       '(3959 * acos(cos(radians('.$options['latitude'].')) * cos(radians('.$model_name.'.latitude)) * cos(radians('.$model_name.'.longitude) - radians('.$options['longitude'].')) + sin(radians('.$options['latitude'].')) * sin(radians('.$model_name.'.latitude)))) AS distance' 
      ), 
      'contain' => array(
       'User','UserInfo', 'GigPostAndCategory.Category','Location','GigPostAndCalender' 

      ), 
      'conditions' => array(
       'GigPost.active' => 1 

      //'OrderGigPost.request' => 0 
      ), 
      'order' => 'GigPost.gig_post_id DESC', 

      'recursive' => 0 
     )); 
     } 

你可以這樣調用:

$query = $this->Model->getAllGigPosts(array('latitude' => XX.XXXXXX,'longitude' => -YY.YYYYYYYYY )); 

或者乾脆

$query = $this->Model->getAllGigPosts(); 
相關問題