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
Post a Comment