I have a model in Cakephp and want to change a Field in beforeSave function but the saved data is not correct.
Here is my function:
public function beforeSave($options = array()) {
$address = @ClassRegistry::init('Address')->read(null, $this->data['Entry']['address_id']);
if($address != false) {
$url_address = $address['Address']['address']." ".$address['Address']['zip']." ".$address['Address']['city'];
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.urlencode($url_address).'&sensor=false');
$output = json_decode($geocode);
$lat = @$output->results[0]->geometry->location->lat;
$lng = @$output->results[0]->geometry->location->lng;
$this->data['Entry']['latitude'] = 0;
$this->data['Entry']['longitude'] = 0;
if($output->status == "OK") {
$this->data['Entry']['latitude'] = $lat;
$this->data['Entry']['longitude'] = $lng;
}
}
return true;
}
The result from GoogleAPI is correct I checked it by writing:
print_r($this->data);
exit();
Only the data written in the DB is not correct. Don't know why - any ideas???
Thx