php - Yii2: Selecting distance and mapping to model -


in projectcontroller, got following function:

public function actionfindnearest($latitude, $longitude, $amount){     $projects = project::findnearest($latitude, $longitude, $amount);     $html = '';     foreach($projects $project){         $html .= $this->renderpartial( '/project/preview', array('model'=>$project), true );     }     return $html; } 

the method in model looks 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)) distance, p.*             project p             order distance limit '.$amount;      $command = yii::$app->db->createcommand($sql);     return $command->queryall(); } 

what array 3 objects containing model attributes plus distance want - perfect! in controller, pass renderpartial now:

foreach($projects $project){         $html .= $this->renderpartial( '/project/preview', array('model'=>$project), true );     } 

in preview.php template, distance attribute lost because have model object, distance isn't official field.

edit: model properties of project class:

/**  * model class 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 achieve use distance in template file?

thank suggestions!

you need add public $distance property project model, , populated automatically each record


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -