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

Commit 94c8ac1

Browse files
committed
Merge pull request #31 from mpoiriert/master
2 parents 09e2039 + e09910f commit 94c8ac1

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/GeocodableBehaviorQueryBuilderModifier.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ public function addFilterByDistanceFrom($builder)
5757
$builder->declareClass('Criteria', 'PDO');
5858

5959
$queryClassName = $builder->getStubQueryBuilder()->getClassname();
60+
$peerClassName = $builder->getStubPeerBuilder()->getClassname();
6061

6162
return $this->behavior->renderTemplate('queryFilterByDistanceFrom', array(
6263
'queryClassName' => $queryClassName,
63-
'defaultUnit' => $this->getDefaultUnit($builder)
64+
'defaultUnit' => $this->getDefaultUnit($builder),
65+
'peerClassName' => $peerClassName,
66+
'longitudeColumnConstant' => $this->behavior->getColumnConstant('longitude_column', $builder),
67+
'latitudeColumnConstant' => $this->behavior->getColumnConstant('latitude_column', $builder),
6468
));
6569
}
6670

src/templates/queryFilterByDistanceFrom.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,27 @@
1212
*/
1313
public function filterByDistanceFrom($latitude, $longitude, $distance, $unit = <?php echo $defaultUnit ?>, $comparison = Criteria::LESS_THAN)
1414
{
15+
if (<?php echo $peerClassName ?>::MILES_UNIT === $unit) {
16+
$earthRadius = 3959;
17+
} elseif (<?php echo $peerClassName ?>::NAUTICAL_MILES_UNIT === $unit) {
18+
$earthRadius = 3440;
19+
} else {
20+
$earthRadius = 6371;
21+
}
22+
23+
$sql = 'ABS(%s * ACOS(%s * COS(RADIANS(%s)) * COS(RADIANS(%s) - %s) + %s * SIN(RADIANS(%s))))';
24+
$preparedSql = sprintf($sql,
25+
$earthRadius,
26+
cos(deg2rad($latitude)),
27+
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>),
28+
$this->getAliasedColName(<?php echo $longitudeColumnConstant ?>),
29+
deg2rad($longitude),
30+
sin(deg2rad($latitude)),
31+
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>)
32+
);
33+
1534
return $this
16-
->withDistance($latitude, $longitude, $unit)
17-
->where(sprintf('Distance %s ?', $comparison), $distance, PDO::PARAM_STR)
35+
->withColumn($preparedSql, 'Distance')
36+
->where(sprintf('%s %s ?', $preparedSql, $comparison), $distance, PDO::PARAM_STR)
1837
;
1938
}

0 commit comments

Comments
 (0)