這篇文章主要介紹了thinkphp5.1的model模型自動更新update_time字段實例講解,文章代碼示例比較簡單實用,有正在學習tp的同學可以跟著小編好好閱讀下
1、model模型開啟自動完成時間戳功能
<?php
namespace app\common\model;
use think\Model;
use think\Db;
class User extends Model{
//開啟自動完成時間戳功能
protected $autoWriteTimestamp = true;
}
?>2、使用update方法更新
User::update(['name'='站長圖庫'],['id'=>1]);
Thinkphp中update方法的源代碼如下:
/**
* 更新數據
* @access public
* @param array $data 數據數組
* @param array $where 更新條件
* @param array|true $field 允許字段
* @return $this
*/
public static function update($data = [], $where = [], $field = null)
{
$model = new static();
if (!empty($field)) {
$model->allowField($field);
}
$result = $model->isUpdate(true)->save($data, $where);
return $model;
}3、使用save方法更新
$user=new User; $user->isUpdate(true)->save(['name'='站長圖庫'],['id'=>1]);






