Fixing Doctrine’s Geographical template precision

Doctrine, an ORM library, has a convenient little behavior that you can attach to your tables to give them awesome superpowers of geolocation. It’s the Geographical template, and it resides in Doctrine/Template/Geographical.php. What it does it add two fields to your table: Latitude and Longitude, and extends your model with the ability to discern distances between any two of these locations.

That’s all well and good, except that as of 1.2.2, Doctrine’s default precision for a floating point field is 2. That doesn’t allow for very accurate geolocation, as it truncates something like “+33.544622″ down to “33.54″. Not good.

The documentation says absolutely nothing about fixing this behavior. I found a bug listing, but no actual fix. Thus began a long session of guesswork, all futile, and ending in a “duhhh” moment and a quick manual trace of the code to determine how the options for templates are handled.

Long story short, I found a fix, and here it is:

YAML version:

actAs:
  Geographical:
    latitude:
      options:
        scale: 7
    longitude:
      options:
        scale: 7

php model version (in your model’s setUp() function):

$geographical0 = new Doctrine_Template_Geographical(array(
    'latitude' =>
    array(
            'options' =>
            array(
                    'scale' => 7,
            ),
    ),
    'longitude' =>
    array(
            'options' =>
            array(
                    'scale' => 7,
            ),
    ),
));
$this->actAs($geographical0);

Post to Twitter Tweet This Post Post to Digg Digg This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Tags:

2 Responses to “Fixing Doctrine’s Geographical template precision”

  1. Axinet says:

    Thanks for tip!

Leave a Reply