In my ProjectController, i got the following function:
public function actionFindNearest($latitude, $longitude, $amount){
$projects = Project::findNearest($latitude, $longitude, $amount);
$html = '';
foreach($projects as $project){
$html .= $this->renderPartial( '/project/preview', array('model'=>$project), true );
}
return $html;
}
The method in the model looks like that:
public static function findNearest($latitute, $longitude, $amount){
$sql = 'SELECT SQRT(
POW(69.1 * (latitude - '.$latitute.'), 2) +
POW(69.1 * ('.$longitude.' - longitude) * COS(latitude / 57.3), 2)) AS distance, p.*
FROM project as p
ORDER BY distance LIMIT '.$amount;
$command = Yii::$app->db->createCommand($sql);
return $command->queryAll();
}
What i get now is an array with 3 objects containing all model attributes plus the distance I want - perfect! In the controller, I pass it to the renderPartial now:
foreach($projects as $project){
$html .= $this->renderPartial( '/project/preview', array('model'=>$project), true );
}
In the preview.php template, the distance attribute is lost because now i only have the model object, where the distance isn't an official field.
Edit: the model properties of the project class:
/**
* This is the model class for table "project".
*
* @property integer $id
* @property string $updated
* @property string $name
* @property string $description
* @property string $teaserimage
* @property string $goal
* @property string $current
* @property string $startdate
* @property string $enddate
* @property string $created
* @property string $headerimage
* @property integer $category_id
* @property integer $address_id
* @property integer $user_id
* @property string $website_url
*
* @property Comment[] $comments
* @property Donation[] $donations
* @property Goodie[] $goodies
* @property Address $address
* @property Category $category
* @property User $user
*/
How could I achieve to use the distance in my template file?
Thank you for your suggestions!
You need to add
public $distance
property into your project model, and then it will be populated automatically for each record