当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp5.1的model模型自动更新update_time字段实例讲解

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-17 09:32:31 浏览: 评论:0 

这篇文章主要介绍了thinkphp5.1的model模型自动更新update_time字段实例讲解,文章代码示例比较简单实用,有正在学习tp的同学可以跟着小编好好阅读下。

1、model模型开启自动完成时间戳功能

  1. <?php 
  2. namespace app\common\model; 
  3. use think\Model; 
  4. use think\Db; 
  5.    
  6. class User extends Model{ 
  7.     //开启自动完成时间戳功能 
  8.   protected $autoWriteTimestamp = true; 
  9. ?> 

2、使用update方法更新

User::update(['name'='安阳'],['id'=>1]);

Thinkphp中update方法的源代码如下:

  1. /** 
  2.   * 更新数据 
  3.   * @access public 
  4.   * @param array   $data 数据数组 
  5.   * @param array   $where 更新条件 
  6.   * @param array|true $field 允许字段 
  7.   * @return $this 
  8.   */ 
  9.   public static function update($data = [], $where = [], $field = null) 
  10.   { 
  11.     $model = new static(); 
  12.     if (!emptyempty($field)) { 
  13.       $model->allowField($field); 
  14.     } 
  15.     $result = $model->isUpdate(true)->save($data$where); 
  16.     return $model
  17.   } 

2、使用save方法更新

$user=new User;

$user->isUpdate(true)->save(['name'='安阳'],['id'=>1]);

Tags: thinkphp5.1 update_time

分享到: