Skip to content
This repository has been archived by the owner on Nov 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #31 from mpoiriert/master
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Aug 1, 2014
2 parents 09e2039 + e09910f commit 94c8ac1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/GeocodableBehaviorQueryBuilderModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ public function addFilterByDistanceFrom($builder)
$builder->declareClass('Criteria', 'PDO');

$queryClassName = $builder->getStubQueryBuilder()->getClassname();
$peerClassName = $builder->getStubPeerBuilder()->getClassname();

return $this->behavior->renderTemplate('queryFilterByDistanceFrom', array(
'queryClassName' => $queryClassName,
'defaultUnit' => $this->getDefaultUnit($builder)
'defaultUnit' => $this->getDefaultUnit($builder),
'peerClassName' => $peerClassName,
'longitudeColumnConstant' => $this->behavior->getColumnConstant('longitude_column', $builder),
'latitudeColumnConstant' => $this->behavior->getColumnConstant('latitude_column', $builder),
));
}

Expand Down
23 changes: 21 additions & 2 deletions src/templates/queryFilterByDistanceFrom.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,27 @@
*/
public function filterByDistanceFrom($latitude, $longitude, $distance, $unit = <?php echo $defaultUnit ?>, $comparison = Criteria::LESS_THAN)
{
if (<?php echo $peerClassName ?>::MILES_UNIT === $unit) {
$earthRadius = 3959;
} elseif (<?php echo $peerClassName ?>::NAUTICAL_MILES_UNIT === $unit) {
$earthRadius = 3440;
} else {
$earthRadius = 6371;
}

$sql = 'ABS(%s * ACOS(%s * COS(RADIANS(%s)) * COS(RADIANS(%s) - %s) + %s * SIN(RADIANS(%s))))';
$preparedSql = sprintf($sql,
$earthRadius,
cos(deg2rad($latitude)),
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>),
$this->getAliasedColName(<?php echo $longitudeColumnConstant ?>),
deg2rad($longitude),
sin(deg2rad($latitude)),
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>)
);

return $this
->withDistance($latitude, $longitude, $unit)
->where(sprintf('Distance %s ?', $comparison), $distance, PDO::PARAM_STR)
->withColumn($preparedSql, 'Distance')
->where(sprintf('%s %s ?', $preparedSql, $comparison), $distance, PDO::PARAM_STR)
;
}

0 comments on commit 94c8ac1

Please sign in to comment.