在表对应的模型文件中,有2个可以设置的固定字段,就是说提交的字段不用设置,只要表中有这两个同名的字段就可以自动插入时间,
创建时间、更新时间;格式可以指定
系统支持自动写入创建和更新的时间戳字段(默认关闭),有两种方式配置支持。
第一种方式是全局开启,在数据库配置文件中进行设置:
- // 开启自动写入时间戳字段
- 'auto_timestamp' => true,
第二种是在需要的模型类里面单独开启:
- <?php
- namespace app\model;
-
- use think\Model;
-
- class User extends Model
- {
- protected $autoWriteTimestamp = true;
支持动态关闭时间戳写入功能,例如你希望更新阅读数的时候不修改更新时间,可以使用isAutoWriteTimestamp
方法:
- $user = User::find(1);
- $user->read +=1;
- $user->isAutoWriteTimestamp(false)->save();
例子:
- protected $autoWriteTimestamp = 'int';//指定数字格式
- protected $createTime = 'time';//创建时间
- // protected $updateTime = 'utime';//更新时间
字段名称也可换成自己的设置
实际例子:
1.分布式数据库插入多表数据,有事务需要回滚
调用:
- //引用
- use app\services\table\model\Guestbook;
-
- //静态调用
- $returnData = Guestbook::saveGuestbook($postdata);
-
表对应模型文件代码:
- <?php
- namespace app\services\table\model;
- use think\Model;
- use think\facade\Db;
- use app\services\table\validate\GuestbookValidate;
-
- class Guestbook extends Model
- {
- protected $name = 'guestbook'; //表名
- protected $connection = 'enterprise_cold';//数据库的连接
-
-
- protected $autoWriteTimestamp = 'int';
- protected $createTime = 'time';
- // protected $updateTime = 'utime';
-
- // 设置字段信息
- protected $schema = [
- 'id' => 'int',
- 'uid' => 'bigint',//(唯一:前端用户取得电脑MAC转为十制进数字)
- 'enterprise_id' => 'int',
- 'phone' => 'string',
- 'username' => 'string',
- 'content' => 'string',
- 'gid' => 'int',
- 'sid' => 'int',
- 'wx' => 'string',
- 'email' => 'string',
- 'qq' => 'int',
- 'url' => 'string',
- 'ip' => 'string',
- 'type' => 'int',
- 'hide' => 'int',
- 'deleted' => 'int',
- 'deleted_time' => 'int',
- 'deleted_userid' => 'int',
- 'time' => 'int',
- ];
-
- /**
- * @name 保存访客提交过来的留言
- * @method Model
- * @date 2021-11-19
- * @param 1 $postdata arr/必填 提交数组
- * @ruturn array
- */
- static public function saveGuestbook(array $postdata)
- {
- Db::connect('enterprise_cold')->startTrans();
- Db::connect('enterprise_heat')->startTrans();
- try {
-
- $postdata['enterprise_id'] = !empty($postdata['eid'])?$postdata['eid']:0;
-
- validate(GuestbookValidate::class)->scene('onlineserviceApi_tel')->check($postdata);
-
- // $eid = !empty($postdata['enterprise_id'])?$postdata['enterprise_id']:0;
- $enterpriseData = numberEncodeDecodeHashids('enterprise',$postdata['enterprise_id'],1);//解密
- if($enterpriseData['code']!=200){
- throw new \Exception("非法提交,互联网不是法外之地,请慎重!");
- }
- //客服分组ID
- if(!empty($postdata['gid'])){
- $enterpriseData = numberEncodeDecodeHashids('onlineservice_group',$postdata['gid'],1);//解密
- if($enterpriseData['code']!=200){
- throw new \Exception("非法提交,互联网不是法外之地,请慎重!");
- }
- }
- //客服ID
- if(!empty($postdata['sid'])){
- $enterpriseData = numberEncodeDecodeHashids('enterprise_userid',$postdata['sid'],1);//解密
- if($enterpriseData['code']!=200){
- throw new \Exception("非法提交,互联网不是法外之地,请慎重!");
- }
- }
-
- $postdata['enterprise_id'] = $enterpriseData['data'][0];
-
- //1.7天表、1个月表
- self::connect('enterprise_heat')->setSuffix('_7d')->save($postdata);//7天表
- self::connect('enterprise_heat')->setSuffix('_1m')->save($postdata);//1个月表
-
- //2.今年表 以前表
- (new static())->setSuffix('_1y')->save($postdata);//今年表
- (new static())->save($postdata);//以前表(所有数据表)
-
- $code=200;$msg="成功";
- Db::connect('enterprise_cold')->commit();
- Db::connect('enterprise_heat')->commit();
- } catch (\Exception $e) {
- Db::connect('enterprise_cold')->rollback();
- Db::connect('enterprise_heat')->rollback();
- $code=-200;$msg=$e->getMessage();
- }
- return ['code' => $code,'msg' =>$msg];
-
- }
-
- }
取得自增ID
- $rs = self::connect('chat_heat')->setSuffix($setSuffix_7d);
- $rs->save($postdata);//7天表
- dump($rs->id);die;
2.单库单表插入数据(有事就启用)
调用:
- //引用
- use app\services\table\model\Guestbook;
-
- //动态调用 要实例化
- $TaskLog =new TaskLog();
- $returnData = $TaskLog ->saveGuestbook($postdata);
表对应模型文件代码:
- <?php
- namespace app\services\table\model;
- use think\Model;
- use think\facade\Db;
- use app\services\table\validate\TaskLogValidate;
-
- /**
- * @name 任务日志
- * @method Model/POST/GET/
- * @date 2021-12-01
- */
- class TaskLog extends Model
- {
- protected $name = 'task_log'; //表名
- protected $connection = 'log';//数据库的连接
-
-
- protected $autoWriteTimestamp = 'int';
- protected $createTime = 'time';
- // protected $updateTime = 'utime';
-
- // 设置字段信息
- protected $schema = [
- 'id' => 'int',
- 'user_id' => 'int',
- 'enterprise_id' => 'int',
- 'title' => 'string',
- 'data' => 'string',
- 'content' => 'string',
- 'starttime' => 'int',
- 'endtime' => 'int',
- 'url' => 'string',
- 'state' => 'int',
- 'url' => 'string',
- 'type' => 'int',
- 'mold' => 'int',
- 'delay' => 'int',
- 'time' => 'int',
- ];
-
-
- /**
- * @name 保存访客提交过来的留言
- * @method Model
- * @date 2021-11-19
- * @param 1 $postdata arr/必填 提交数组
- * @ruturn array
- */
- public function DataSave(array $postdata)
- {
- // Db::connect($this->connection)->startTrans();//有回滚就启用
- try {
-
-
- // if(empty($postdata)){
- // throw new \Exception("提交数据为空!");
- // }
- $postdata['enterprise_id'] = !empty($postdata['eid'])?$postdata['eid']:0;
-
- validate(TaskLogValidate::class)->check($postdata);
-
- if(!empty($postdata['enterprise_id'])){
- $enterpriseData = numberEncodeDecodeHashids('enterprise',$postdata['enterprise_id'],1);//解密
- if($enterpriseData['code']!=200){
- throw new \Exception("非法提交,互联网不是法外之地,请慎重!");
- }
- $postdata['enterprise_id'] = $enterpriseData['data'][0];
- }
-
- $this->save($postdata);//以前表(所有数据表)
-
- $code=200;$msg="成功";
- // Db::connect($this->connection)->commit();
- } catch (\Exception $e) {
- // Db::connect($this->connection)->rollback();
- $code=-200;$msg=$e->getMessage();
- }
- return ['code' => $code,'msg' =>$msg];
-
- }
-
- }