Search code examples
symfony-1.4geocodingpropelbehavior

Implementing GeocodableBehavior in Symfony 1.4 (using Propel)


I'm trying to implement the GeocodableBehavior on a Symfony 1.4 (with Propel 1.6) project i'm working on, but until now it's a complete failure. I've tried to search if other people but I didn't found anything, like if I was the only one having troubles with this.

So, maybe I'm missing something very very easy, but following the instructions given on the GeocodableBehavior leads to nothing but errors, and I can't figure out where's the problem.

I followed instructions for the GeocodableBehavior (here -> http://www.propelorm.org/cookbook/geocodable-behavior.html)

This seems to work as i'm getting the latitude/longitude columns created on my model. Until then, it works fine.

Where things get a little more complicated is when trying to save an object with the GeocodableBehavior, there's problems with the Geocoder class.

(Documentation here -> https://github.com/willdurand/Geocoder)

My class is Point, referring to a geolocated point, an address. When creating a Point using sf admin generator, the behavior which is supposed to use some fields (street, postal_code, country, etc) to query the GoogleMaps api, just fails to use the Geocoder class.

Fatal error: Class 'Geocoder\Geocoder' not found in /var/www/vhosts/www._________.local/lib/model/om/BasePoint.php on line 3717

I put the Geocoder class in a lib/vendor/geocoder folder, I tried to use the autoload.yml file to load it, but nothing changes...

autoload:
  geocoder:
    name:       geocoder
    path:       %SF_LIB_DIR%/vendor/geocoder
    recursive:  on

There's something i'm missing in how to load those classes in my sf project, and i can't find what. Geocoder package has an autoload.php file but i didn't manage to "load" it successfully...

Thanks in advance.


Solution

  • I know it's kinda giving up on the autoloader, but you could establish a register function in /config/ProjectConfiguration.class.php. The only downside is that you will need to add a call to the function before any block that uses Geocoder.

    class ProjectConfiguration extends sfProjectConfiguration
    {
        static protected $geocoderLoaded = false;
    
        static public function registerGeocoder()
        {
            if (self::$geocoderLoaded) {
                return;
            }
    
            require_once sfConfig::get('sf_lib_dir') . '/vendor/geocoder/autoload.php';
    
            self::$geocoderLoaded = true;
        }
        ...
    }
    

    Then just execute ProjectConfiguration::registerGeocoder(); anywhere you'd need the class. It's more annoying than getting the autoloader to work, but it's at least dependable.