SQL-query calculation as property

I want to add a property to an attribute of a model based on a SQL-query. However, the SQL-query will make a comparison to the preceding element of the property-array.

More concretely: the model is walks which has many waypoints. I want to calculate the distance between each waypoint via SQL (see below). Two questions:

  1. how to attach $distance (see model) to a property?

  2. how do I access the lat/long property of the previous waypoint?

The code

Controller: WalksController.php

	public function relations()


		return array(

			'waypoints' => array(self::HAS_MANY, 'Waypoints', 'walk_id', 'order'=>'waypoints.order_id ASC'),



Model: Waypoint.php


	 *  calculates the distance between two sets of lat/long coordinates

	 * @return array distance in km and miles


	public function distanceBetween(){





											  SIN(RADIANS( ".$this->lat." )) * SIN(RADIANS( ".$lat2." ))

												  +  COS(RADIANS( ".$this->lat." )) * COS(RADIANS( ".$lat2." ))

												  * COS(RADIANS( ".$this->lon." - ".$lon2." ))

											  ) * 60 * 1.1515


										) AS distance"); //returns miles


		return $distance;


The terminology might not be accurate, as my OOP & MVC is a bit rusty and I am just getting back into it thanks to Yii :)

If you simply rename your function in getDistance, you can call on your class $model->distance.

That is how to make new property to objects (you can write a setter by writing setProperty($data)).

Is that what you need?

Works like a charm. Thanks :)