thinkphp3 增、改函数封装

新增数据

新增数据,提供数据,表名,是否调试

/**
 * @title  新增数据记录
 * @author lyj [author] [2018-06-16]
 * @param  [type]  $data  [数据]
 * @param  [type]  $table [表名]
 * @param  integer $debug [是否调试]
 */
function addData($data, $table, $debug = 0)
{
    if($debug == 0){
        // 生成记录
        $data_res = M($table)->add($data);

        // 新增失败
        if(!$data_res){
            $log = "$table新增数据失败 ".getsqlstr(1);

            lyjlog($log);

            M()->rollback();            
        } else {             
            return true;
        }        
    } else {
        dump($data);
    }
}

修改数据

引用块内容,提供数据(含有主键),表名,是否调试

/**
 * @title  更新数据记录
 * @author lyj [author] [2018-06-16]
 * @param  [type]  $data  [description]
 * @param  [type]  $table [description]
 * @param  integer $debug [description]
 */
function updateData($data, $table, $debug = 0){
    if($debug == 0){
        // 生成记录
        $data_res = M($table)->save($data);

        // 更新失败
        if(!$data_res){
            lyjlog("$table更新数据失败 ".getsqlstr(1));

            M()->rollback();            
        } else {            
            return true;
        }        
    } else {
        dump($data);
    }
}
posted @ 2018-07-03 09:38  tang1jun  阅读(251)  评论(0编辑  收藏  举报