wikipoints/models/Airports.php

50 lines
1.0 KiB
PHP

<?php
class Airports extends Model
{
function __construct($fromArray = array())
{
$this->_tableName_ = 'airports';
parent::__construct($fromArray);
}
static function model()
{
return new self();
}
public static function getNearest($lat, $lng)
{
$degrees = 3;
$lat = floatval($lat);
$lng = floatval($lng);
$delta = (1+abs(SIN($lat/100)*SIN($lat/100)*5))*$degrees;
$query = '
SELECT *,
(POW(`lat`-:lat,2)+POW(`lng`-:lng,2)) AS `dist`
FROM `airports`
WHERE
`lat` BETWEEN :minLat AND :maxLat
AND
`lng` BETWEEN :minLng AND :maxLng
ORDER BY `dist`
LIMIT 1;';
$res = App::DB()->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$res->execute(array(':lat' => $lat, ':lng' => $lng, ':minLat' => $lat - $degrees, ':minLng' => $lng - $delta, ':maxLat' => $lat + $degrees, ':maxLng' => $lng + $delta));
if ($res && $row = $res->fetch(PDO::FETCH_ASSOC)) {
return new Airports($row);
}
return null;
return self::model()->getAll(array('where'=>'`lat`>0 AND `lng`>0', 'order','limit'=>1));
}
}