Extending Propel -- Integrating Zip Code Distance Calculation

For the past day I've been working a lot with zip code distance calculation, and now it's time to integrate my findings into my application. I've chosen to build my application around the Propel framework, so my code will be added to the application classes which Propel so graciously generates skeletons for.

We'll start out with the Event class. We want to get all events within n miles of a particular zipcode. Since this is generalized beyond the level of Event, we will place it within the EventPeer class (for more information, read Propel's documentation).

class EventPeer extends BaseEventPeer {

static function GetWithinRadius( $zip, $maxDistance ) {
$con = Propel::getConnection(self::DATABASE_NAME);

// get a list of columns that we actually care about (sort of hackish, but not really. see Select Using SQL in Propel's user guide)
$c = new Criteria();
self::addSelectColumns( $c );

$sql = "SELECT ".join( ",", $c->getSelectColumns() )." FROM ".self::TABLE_NAME." WHERE ZipDistance(?, ".self::ZIPCODE.") < ?"; $stmt = $con->prepareStatement( $sql );
$stmt->setInt( 1, $zip );
$stmt->setInt( 2, $maxDistance );

$rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM);

return self::populateObjects($rs);
}

}


Easy enough? yep. We implement UserPeer::GetWithinRadius exactly the same. Now as an exercise to show you how fun Propel can be, here is a function to get all events within n miles of a particular User:

class User extends BaseUser {
function getNearByEvents( $maxDistance ) {
$zip = $this->getZipCode();

return EventPeer::GetWithinRadius( $zip, $maxDistance );
}
}


This is ridiculously simple, isn't it? I'd like to give thanks to the inventors of stored procedures and Propel.