Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Accessing Database 'as' Value From Cactivedataprovider

$
0
0
Hello,

I have the following model that serves to represent a store's location, included in that location are it's longitude and latitude. I want a user to be able to search by their location and be given a list of nearest stores and the distance they currently are from that location. I am able to return a list of nearest stores in sorted order from the following code, however, I do not know how to extract the 'distance' value that is set from the 'AS' MySQL query. Ideally I would return this calculated 'distance' as if it was a property of each model.

Is there a way to get 'AS' values from an SQL query within yii?


class Location extends CActiveRecord
{

   ...

   public function search()
   {
      $criteria = new CDbCriteria;

      	// special calculation for distance
	if( isset( $this->latitude ) && isset( $this->longitude ) ){
		$criteria->params = array(
			':curr_lat' => $this->latitude,
			':curr_lon' => $this->longitude,
		);
		// The Haversine Formula, used to calculate lat long distances from two locations
		$criteria->select = 
			"*, 
			( 	
				3959 * acos( 
					cos( radians(:curr_lat) ) * 
					cos( radians( latitude ) ) * 
					cos( radians( longitude ) - radians(:curr_lon) ) + 
					sin( radians(:curr_lat) ) * 
					sin( radians( latitude ) ) 
				) 
			) AS distance    // this is the value I would like to return with the found models
		";
		$criteria->order = '`distance` ASC';
	}

	$criteria->limit = 10;

	return new CActiveDataProvider($this, array(
		'criteria'=>$criteria,
	));
   }

   ...

}



Any suggestions?

Thanks!!

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>