我在 Cakephp 中有一个模型,想更改 beforeSave 函数中的字段,但保存的数据不正确。
这是我的功能:
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;
}
GoogleAPI 的结果是正确的,我通过以下方式对其进行了检查:
print_r($this->data);
exit();
只有写入 DB 的数据不正确。不知道为什么 - 任何想法???
谢谢