记一次错误使用 laravel 模型的 updated_at
有个表的数据需要拉取接口来更新同步,原本设想的是每次调用模型的 save
方法会更新 updated_at
,实际上如果数据没有变化,updated_at
是不变的..
尤其以 updated_at
做为缓存时间判断的,要注意。
class Test extends Model {
public static function autoUpdateGet($uid) {
$expire = 3 * 3600; // 3小时更新一次
$test = self::where('uid', $uid)->first();
if (time() - strtotime($test->updated_at) < $expire) {
return $test;
}
//name不发生变化,updated_at字段不会变化,一直会走这段代码
$data = curl('xxx');
$test->name = $data['name'];
$test->save();
return $test;
}
}
改为
$test->name = $data['name'];
$test->updated_at = date('Y-m-d H:i:s',time());
$test->save();
推荐文章: