重识TP5中模型
创建一个表MODEL,下面展现代码片段:
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(30) NOT NULL DEFAULT '' COMMENT '名称', `pid` int unsigned NOT NULL DEFAULT 0 COMMENT '父级ID', `tid` int unsigned NOT NULL DEFAULT 0 COMMENT '顶级ID', `tree` varchar(255) NOT NULL DEFAULT '' COMMENT '层级树(,1,2,)', `status` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '状态', `sort` int unsigned NOT NULL DEFAULT 100 COMMENT '排序', `is_top` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '是否顶置', `create_ts` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间', `update_ts` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '修改时间', `create_user` int unsigned NOT NULL DEFAULT 0 COMMENT '创建用户', `update_user` int unsigned NOT NULL DEFAULT 0 COMMENT '修改用户', `remark` text COMMENT '详情描述(json)', PRIMARY KEY (`id`),
添加一条数据的时候,只初始化赋值的值,对于未赋值的值(除ID外)会抛出类的属性不存在异常:
$a = new AutoTypeModel(); $a->data(['name'=>'hehe','tree'=>'tree']); echo "\n"; echo $a->name; // hehe echo "\n"; echo $a->tree; // tree echo "\n"; echo $a->save(); // 1 echo "\n"; echo $a->id; // 41 echo "\n"; echo $a->create_ts; // 类的属性不存在:app\auto\model\AutoTypeModel->create_ts echo "\n"; echo $a->update_ts; // 类的属性不存在:app\auto\model\AutoTypeModel->update_ts echo "\n"; echo $a->pid; // 类的属性不存在:app\auto\model\AutoTypeModel->pid exit;
注意:如果开启自动写入时间戳字段,那么会自动赋值到 create_ts,update_ts,就不会抛出这两个字段的异常啦!
// 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; // 定义时间戳字段名 protected $createTime = 'create_ts'; protected $updateTime = 'update_ts';
那么换种方式:
$a = new AutoTypeModel(); $a->data(['name'=>'hehe','tree'=>'tree']); $a->pid = 3; echo "\n"; echo $a->name; // hehe echo "\n"; echo $a->tree; // tree echo "\n"; echo $a->save(); // 1 echo "\n"; echo $a->id; // 52 echo "\n"; echo $a->create_ts; // 2018-12-19 16:54:40 echo "\n"; echo $a->update_ts; // 2018-12-19 16:54:40 echo "\n"; echo $a->pid; // 3 exit;
修改一条数据:
$a = AutoTypeModel::get(20); // $a->id = 20; $a->name = "oo2"; echo "\n"; echo $a->name; echo "\n"; echo $a->tree; echo "\n"; echo $a->id; echo "\n"; echo $a->create_ts; echo "\n"; echo $a->update_ts; echo "\n"; echo $a->pid; echo "\n"; echo $a->save(); exit;
OR
// 闭包修改 $a = AutoTypeModel::get(function ($query){ $query->where('id',20); }); // $a->id = 20; $a->name = "oo1"; echo "\n"; echo $a->name; echo "\n"; echo $a->tree; echo "\n"; echo $a->id; echo "\n"; echo $a->create_ts; echo "\n"; echo $a->update_ts; echo "\n"; echo $a->pid; echo "\n"; echo $a->save(); exit;
同时修改多条数据,如下写法请注意只能修改一条数据哦,证明 new Model 的时候只对一条或者一行数据有效:
// 闭包修改 $a = AutoTypeModel::get(function ($query){ $query->whereIn('id','20,21,22'); });
那么可以将 new Model 想象成数据库的一条数据对它操作把,如果要操作多条数据,那么可以 new Model 多次
$a = AutoTypeModel::get(20); $b = AutoTypeModel::get(21); $c = AutoTypeModel::get(22); $name = "oo"; $a->name = $name; $b->name = $name; $c->name = $name; echo "\n"; echo $a->save(); // 1 echo "\n"; echo $b->save(); // 1 echo "\n"; echo $c->save(); // 1 exit;
当然也可以使用:
$a = new AutoTypeModel; echo "\n"; echo $a->save(['name'=>'aaaa'],['id'=>['in','20,21,22']]); // 3 exit;
原创文章请随便转载。愿和大家分享,并且一起进步。-- 江 coder