thinikphp 将数据库模型的增、删、改操作写入日志 -凯发k8官方网
凯发k8官方网
收集整理的这篇文章主要介绍了
thinikphp 将数据库模型的增、删、改操作写入日志
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
thinkphp中的模型可以对数据库字段进行验证规则的设置和设置一些字段的默认值(比如字段为当前时间)以及在操作数据时的的一些回调方法等 基本上每一个模型都需要设置一些验证规则和字段默认值的设置,而大部分都存在着重复的工作 特别是像需要将数据库操作记录到日志系统的,这就导致我们需要在每个模型中反复处理 针对此问题我定义一个父类模型,所以模型都继承自此类即可解决以上两个问题 父类就完成了以下两件工作: 记录增、删、改等操作到日志系统 定义一些大部分数据库都需要验证的规则(子类可覆盖或自定义)或者需要自动生成的字段(如每个数据库都有一个记录当前时间的字段create_time) _validate;}/*** @author: htl* @email: huangyuan413026@163.com* @datetime: 2016-04-08 12:06:03* @description: 获取当前时间*/protected function mgetdate() {return date ( 'y-m-d h:i:s' );}/*** @author: htl* @email: huangyuan413026@163.com* @datetime: 2016-04-08 10:50:20* @description: 更新成功后的回调方法*/protected function _after_update($data,$options) {//区分会员登录和更改操作if( $options['model']=="users"&& $data['last_login_ip']&& $data['last_login_time']&& count($data)==3){$this->after_write("login",$data,$options);}else{$this->after_write("update",$data,$options);}}/*** @author: htl* @email: huangyuan413026@163.com* @datetime: 2016-04-08 10:50:27* @description: 插入成功后的回调方法*/protected function _after_insert($data,$options) {$this->after_write("insert",$data,$options);}/*** @author: htl* @email: huangyuan413026@163.com* @datetime: 2016-04-08 11:56:12* @description: 删除成功后的回调方法*/protected function _after_delete($data,$options) {$this->after_write("delete",$data,$options);}/*** @author: htl* @email: huangyuan413026@163.com* @datetime: 2016-04-08 11:03:18* @description: 更新或插入成功后和删除前写入系统日志*/function after_write($type,$data,$options){$db_name = c('db_prefix')."system_log"; //日志表//如果是系统日志表则不处理,防止循环调用此方法if(!$this->_is_array($data) || !$this->_is_array($options) || strcasecmp($options['table'],$db_name)==0) return ;$model = m("systemlog"); //日志表$new_value = json_encode($data);// 去除前缀的表名$_data['log_table'] = str_replace(c('db_prefix'), "",$options['table']);//更改时如果原数据未更改则不进行记录,防止重复记录if("update" === $type){//表主健$_data['t_id'] = $data['id'];// 主健名称不是id// 获取主健对应的数据if($_data['id']){$tablename = $options['table'];$_data['t_id'] = $data[m($tablename)->getpk()];}// 如果最后一条的值没有更改则不记录if($model->where($_data)->order("id desc")->getfield("new_value")===$new_value) return;}$_data['log_type'] = $type;$_data['new_value'] = $new_value;$_data['log_user'] = $_session['admin_id'];$_data['create_time'] = date('y-m-d h:i:s');$_data['ip_address'] = get_client_ip(0,true);$_data['user_agent'] = $_server['http_user_agent'];try {$model->add($_data);} catch (exception $e) {\think\log::write('写入系统日志时发生错误,错误信息:'.$e->getmessage(),'warn');}}/*** @author: htl* @email: huangyuan413026@163.com* @datetime: 2016-04-08 11:04:40* @description: 是否是数组*/function _is_array($array){return ($array && is_array($array) && count($array)>0);}
}
定义一个数据库模型并继承自commonmodel即可 _validate = array_merge (parent::get_validate(),$_val);// 移出父类的code唯一性验证//foreach ($this->_validate as $key => $value) {// if($value[0]=='code' && $value[4]=='unique'){// unset($this->_validate[$key]);// }//}//覆盖父类验证规则$this->_validate = $_val;}
}
数据库结构 create table `tp_system_log` (`id` int(11) not null auto_increment,`log_type` varchar(50) not null comment '操作类别',`log_table` varchar(100) not null comment '操作的表',`log_user` varchar(100) not null comment '操作的用户',`t_id` varchar(50) not null comment '操作的表的主健id',`create_time` datetime not null default current_timestamp comment '操作的时间',`new_value` text not null comment '操作后的新值',`ip_address` varchar(20) not null comment 'ip地址',`user_agent` varchar(500) null default null comment 'user-agent:',primary key (`id`),index `id` (`id`)
)
参考: tp3.2开发手册 自动验证
tp3.1开发手册 模型扩展
总结
以上是凯发k8官方网为你收集整理的thinikphp 将数据库模型的增、删、改操作写入日志的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: la3971 组装电脑
- 下一篇: