Skip to content

Instantly share code, notes, and snippets.

@garak
Created May 27, 2011 12:50
Show Gist options
  • Save garak/995167 to your computer and use it in GitHub Desktop.
Save garak/995167 to your computer and use it in GitHub Desktop.
<?php
// lib/model/doctrine/ItemTable.php
class ItemTable extends Doctrine_Table
{
/**
* @param array $values
* @param integer $limit
* @return Doctrine_Collection
*/
public function search($values, $limit = 10)
{
$lat = floatval($values['latitude']);
$lng = floatval($values['longitude']);
// replace 6371 with 3959 if you want miles instead of kilometres
return $this->createQuery()
->select('*, (6371 * acos(cos(radians(' . $lat . ')) * cos(radians(latitude)) * cos(radians(longitude) - radians(' . $lng . ')) + sin(radians(' . $lat . ')) * sin(radians(latitude)))) AS distance')
->where('distance <= ?', $values['radius'])
->orderBy('distance')
->limit($limit)
->execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment