Hello Everyone,
can anybody explain me, how can I add own computed property to my result that is ActiveDataProvider.
I have function that return objects near me (objects/near?lat=X&lng=Y) and I would like to add field distance that consist distance between my current coordinates and object’s coordinates:
public function actionNear($lat, $lng)
{
//For test purpose
$lat = 55.7541072;
$lng = 37.6204735;
$query = Object::nearMe($lat, $lng);
return $query;
}
In my model Object I have nearMe function:
public static function nearMe($lat, $lng) {
if (!isset($lat) && !isset($lng)) return false;
$result = Object::find()
->select([
'object_id',
'lt',
'lg',
'title',
'distance' => 'SQRT(POWER(((lt - '. $lat .')*COS(RADIANS(lt))*40000/360), 2) + POWER(((lg - '. $lng .')*40000/360), 2))'
])
->where('')
->orderBy(['distance' => 'ASC']);
return new ActiveDataProvider([
'query' => $result,
'pagination' => [
'pageSize' => $pagesize,
],
]);
}
Field distanse not added in such request (don’t now why, just direct sql request working correcly.
I tried to add distance in fields() method of model, but can’t understand how can I push parameters lang and lat from request to fields. What is a correct way that helps to add in result own computed fields based on result from DB for each item ?