How to make find all query in cakephp conditions with greater than & less than equal to a values

Ankit picture Ankit · Dec 21, 2014 · Viewed 11.3k times · Source

I have to fetch all data from table which has latitudes / longitudes values.Actually I just required all latitudes / longitudes values which is within 1000 miles from a source, So for that I used concept & it's calculating latitudes / longitudes properly but when I am creating a find query to fetch data which has table.latitudes <= latitudes North and table.latitudes >= latitudes South

Similarly for longitudes.

I have made these calculation code & find query

 $lat1 = $coodSearch['lat'];
       $lon1 = $coodSearch['long'];
       $d = $coodSearch['dis'];
       $r = 3959;//earth's radius in miles

          //compute max and min latitudes / longitudes for search square
                $latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
                $latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
                $lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
                $lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));

          $this->loadModel('City');
         $finder1 = array( 'and' => array('City.latitude <=' => $latN,'City.latitude >=' => $latS ));
         $latArr = $this->City->find('all', array('conditions' => $finder1));
         $finder2 = array( 'and' => array('City.longitude >=' => $lonE,'City.longitude <=' => $lonW ));
         $longArr = $this->City->find('all', array('conditions' => $finder2));

I have checked Calculated value($latN,$lonE) its giving result but find all query is not working.

Please help me where I am going wrong.

Answer

Keyur Padalia picture Keyur Padalia · Dec 23, 2014

This might do the trick

$data = $this->ModelName->find('all', array('conditions' => array('ModelName.DbColumn >=' => 10)));
$data = $this->ModelName->find('all', array('conditions' => array('ModelName.DbColumn <=' => 10)));
$data = $this->ModelName->find('all', array('conditions' => array('ModelName.DbColumn >=' => 10,'ModelName.DbColumn <=' => 20)));