0

私はcakephpの問題に取り組んでいます。私はcakephpでクエリを実行する方法を知っています。特定の状況では、sqlステートメント自体をquery($ sql)として使用する必要があります。以下は、データベースから最も近いポイントを取得するためのクエリです。SQLは正常に動作します。今、私はそれをcakephpスタイルにして、一般的なページネーションフォームcakephpを追加したいと思います。残念ながら、その方法に関する情報が見つかりません。一般的にそれを行うための情報を見つけるのは簡単ではありません。だから私はあなたからの解決策を持ちたくありません、私は複雑なクエリをcakephpに変換する方法のリンクを手に入れたいと思います。

これが私のコードスニペットです(すでに述べたように機能しています)。

    public function getRestAroundMe($lat, $lng, $radius, $limit) {  // Get some data to validate login.
        /* Source: http://funkjedi.com/technology/308-search-using-geolocation-data-in-mysql/ */

        // Constants related to the surface of the Earth
        $earths_radius = 6371;
        $surface_distance_coeffient = 111.320;

        // Spherical Law of Cosines
        $distance_formula = sprintf('%s * ACOS( SIN(RADIANS(geolatitude)) * SIN(RADIANS(%s)) + COS(RADIANS(geolongitude - %s)) * COS(RADIANS(geolatitude)) * COS(RADIANS(%s)) )', $earths_radius, $lat, $lng, $lat); 

        // Create a bounding box to reduce the scope of our search
        $lng_b1 = $lng - $radius / abs(cos(deg2rad($lat)) * $surface_distance_coeffient);
        $lng_b2 = $lng + $radius / abs(cos(deg2rad($lat)) * $surface_distance_coeffient);
        $lat_b1 = $lat - $radius / $surface_distance_coeffient;
        $lat_b2 = $lat + $radius / $surface_distance_coeffient;

        // Construct our sql statement
        $sql = sprintf('SELECT *, (%s) AS distance FROM restaurants AS Restaurant WHERE (geolatitude BETWEEN %s AND %s) AND (geolongitude BETWEEN %s AND %s) HAVING distance < %s ORDER BY distance ASC LIMIT 0, %s', $distance_formula, $lat_b1, $lat_b2, $lng_b1, $lng_b2, $radius, $limit);

        return $this->query($sql);
}

助けてくれてありがとう

4

2 に答える 2

1

今、私は私の解決策を持っています:

    public function geoRest($lat, $lng, $radius, $limit) {
        /* Source: http://funkjedi.com/technology/308-search-using-geolocation-data-in-mysql/ */

        // Constants related to the surface of the Earth
        $earths_radius = 6371;
        $surface_distance_coeffient = 111.320;

        // Spherical Law of Cosines
        //$distance_formula ='$earths_radius * ACOS( SIN(RADIANS(geolatitude)) * SIN(RADIANS($lat)) + COS(RADIANS(geolongitude - $lng)) * COS(RADIANS(geolatitude)) * COS(RADIANS($lat)) )';
        $distance_formula = sprintf('%s * ACOS( SIN(RADIANS(geolatitude)) * SIN(RADIANS(%s)) + COS(RADIANS(geolongitude - %s)) * COS(RADIANS(geolatitude)) * COS(RADIANS(%s)) )', $earths_radius, $lat, $lng, $lat); 
        // Create a bounding box to reduce the scope of our search
        $lng_b1 = $lng - $radius / abs(cos(deg2rad($lat)) * $surface_distance_coeffient);
        $lng_b2 = $lng + $radius / abs(cos(deg2rad($lat)) * $surface_distance_coeffient);
        $lat_b1 = $lat - $radius / $surface_distance_coeffient;
        $lat_b2 = $lat + $radius / $surface_distance_coeffient;

        // Construct our sql statement
        //$sql = sprintf('SELECT *, (%s) AS distance FROM restaurants AS Restaurant WHERE (geolatitude BETWEEN %s AND %s) AND (geolongitude BETWEEN %s AND %s) HAVING distance < %s ORDER BY distance ASC LIMIT 0, %s', $distance_formula, $lat_b1, $lat_b2, $lng_b1, $lng_b2, $radius, $limit);
        //return $this->query($sql);
        //$options['fields'] = array('*',$distance_formula.' AS distance');

        $this->virtualFields['distance'] = $distance_formula;
        $options['fields'] = array('*');
        $options['conditions'] = array(
                            'Restaurant.geolatitude BETWEEN ? AND ?' => array($lat_b1, $lat_b2),
                            'AND' => array('Restaurant.geolongitude BETWEEN ? AND ?' => array($lng_b1, $lng_b2)),
                            'AND' => array('NOT' => array('Restaurant.geolongitude' => 0)),
                            'AND' => array('NOT' => array('Restaurant.geolatitude' => 0)),
                            'AND' => array('NOT' => array('Restaurant.geolongitude' => null)),
                            'AND' => array('NOT' => array('Restaurant.geolatitude' => null))
                            );
        $options['contain'] = array(
                            'Menu' => array(
                                            'conditions' => array(
                                                                  'Menu.active' => '1'
                                                                  )
                                            )
        );

        $options['order'] = 'Restaurant.distance ASC';
        $options['limit'] = $limit;

        //return $options;

        return $this->find('all', $options);

残念ながら「含む」は機能しませんが、それは私が取り組んでいる別の問題です。誰かがそれに対する解決策を持っているなら、それは素晴らしいことです!

于 2013-03-18T20:14:17.663 に答える
0

Custom query paginationを見てください。カスタム クエリを使用してページ分割する方法はいくつかあります。

于 2013-03-04T11:48:37.647 に答える