diff --git a/Doctrine/ORM/GeocoderListener.php b/Doctrine/ORM/GeocoderListener.php index 3194aac9..9d73f961 100644 --- a/Doctrine/ORM/GeocoderListener.php +++ b/Doctrine/ORM/GeocoderListener.php @@ -86,12 +86,19 @@ private function geocodeEntity($entity) { $metadata = $this->driver->loadMetadataFromObject($entity); $address = $metadata->addressProperty->getValue($entity); - $results = $this->geocoder->geocodeQuery(GeocodeQuery::create($address)); - - if (!empty($results)) { - $result = $results->first(); - $metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude()); - $metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude()); + if (!empty($address)) { + try { + $results = $this->geocoder->geocodeQuery(GeocodeQuery::create($address)); + + if (!empty($results)) { + $result = $results->first(); + $metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude()); + $metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude()); + } + } catch(\Exception $e) { + // todo log? + } } + } } diff --git a/Mapping/Driver/AnnotationDriver.php b/Mapping/Driver/AnnotationDriver.php index b8ec909e..967e789f 100644 --- a/Mapping/Driver/AnnotationDriver.php +++ b/Mapping/Driver/AnnotationDriver.php @@ -31,14 +31,27 @@ public function __construct(Reader $reader) public function isGeocodeable($object): bool { - $reflection = new \ReflectionObject($object); + $reflection = new \ReflectionClass($object); + + // check if object is a doctrine proxy object + if ($object instanceof \Doctrine\Common\Persistence\Proxy) { + // This gets the real object, the one that the Proxy extends + $reflection = $reflection->getParentClass(); + } return (bool) $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class); } public function loadMetadataFromObject($object) { - $reflection = new \ReflectionObject($object); + $reflection = new \ReflectionClass($object); + + // check if object is a doctrine proxy object + if ($object instanceof \Doctrine\Common\Persistence\Proxy) { + // This gets the real object, the one that the Proxy extends + $reflection = $reflection->getParentClass(); + } + if (!$annotation = $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class)) { throw new Exception\MappingException(sprintf( 'The class %s is not geocodeable', get_class($object)