Hi,
Im writing an API with YII tutorial "How-To: Create a REST API", but Im with some troubles with actionList.
I have one table Profile, that I would like to list only the field Name and the Distance (calculated between 2 coordinates.)
The problem is that the response of Json is returning all fields of the table (as null).
I have already declared "public $distance;" in Profile Model, and its accesible by $model->distance.
I would like to know how can I rewrite the foreach to get only the "select" fields from CActiveDataProvider.
As I use it in others tables, should be a generic way.
Here is my code:
public function actionList() {
...
$criteria = new CDbCriteria;
$criteria->select = 'name, ( 3959 * acos( cos( radians(' . $latitude . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $longitude . ') ) + sin( radians(' . $latitude . ') ) * sin( radians( lat ) ) ) ) * 1.609344 AS distance';
$models = new CActiveDataProvider('Profile', array(
'criteria' => $criteria,)
);
foreach($models as $model){
$rows[] = $model->attributes;
}
$this->_sendResponse(200, CJSON::encode($rows));
...
}
Json Response: (Not returning the distance.)
[{"name":"Profile 1","id":null,"email":null,"website":null,"telephone":null,"address1":null,"address2":null,"postcode":null,"city":null,"state":null,"status":null,"lat":null,"lng":null,"created":null,"modified":null}]