Skip to content

Instantly share code, notes, and snippets.

@fetus-hina
Created December 13, 2012 18:40
Show Gist options
  • Save fetus-hina/4278593 to your computer and use it in GitHub Desktop.
Save fetus-hina/4278593 to your computer and use it in GitHub Desktop.
「Yii Framework の AR で関数通した結果を取得できないの?」とか言ってた結果現在こうなってる
<?php
class Stations extends CActiveRecord {
public $distance;
// ^^^^^^^^^^^^^^^^^
// (omit)
public function byLocation($lat, $long, $limit = 2000) {
$self = $this->getTableAlias(false, false);
$func = "distance({$self}.location, POINT(:latitude, :longitude))";
$this->getDbCriteria()->mergeWith(array(
'select' => array('*', 'distance' => $func),
// ^^^^^^^^^^^^^^^^^^^^^
'condition' => "{$func} <= :distance_limit",
'params' => array(':latitude' => (float)$lat, ':longitude' => (float)$long, ':distance_limit' => $limit),
'order' => "{$func} ASC",
));
return $this;
}
// (omit)
}
?>
「プロパティ」にしてやれば CActiveRecord::populateRecord が見つけてなんとかしてくれる
が、この "scope" を使う時のためだけにこれを作るのはダサいと思うんだ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment