Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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