|
本帖最后由 周大胖子 于 2018-8-15 07:17 编辑
【等会 我暂时还没想出来什么场景用最合适 因为设置该值的时候,动态赋值就失效了】
1.$insert = ['字段'=>'值',------] ; // 新增时自动插入到表中的
2.$update = ['字段'=>‘值’,---] ; //更新时自动更新字段值
3.$auto = ['字段'=>‘值’,---] ; //新增或更新时自动填入或更新字段值
通常要与类型自动转换属性: $type = ['字段'=>'类型',----]相配合
Staff.php- // 保存自动完成列表
- protected $auto = [
- 'age' => 36,
- ];
- // // 新增自动完成列表
- // protected $insert = [
- // 'age'=>18, // 添加时默认年龄是18
- // ];
- // // 更新自动完成列表
- // protected $update = [
- // 'gongzi'=>2500
- // ];
复制代码
controllder/Dcm.php- <?php
- namespace app\articles\controller;
- use app\articles\model\Staff; //先引入 staff 类;
- header("Content-type: text/html; charset=utf-8");
- // 自动时间戳功能
- // 1.模型中开启该功能: protected $autoWriteTimestamp=true;
- //
- // 2.表中手动创建两个字段:create_time ,update_time;
- // 3.用户执行新增与更新操作时,会自动将新增与更新写入表中
- // 4.默认以时间戳格式进行写入,可以配置为:datatime 格式
- // 自动完成功能
- class Dcm
- {
- public function mn1()
- {
- return '进来了 }
- // 先添加
- public function mn2()
- {
- $lma = Staff::create($data=[
- 'name'=>'张婷',
- 'age'=>'29',
- 'juese'=>'市场部',
- 'gongzi'=>3500,
- ]);
- dump($lma->getData());
- }
- // 改动
- public function mn3()
- {
- $data = [
- 'gongzi'=>5001,
- 'age'=>32
- ];
- $where =[
- 'id'=>33
- ];
- $lma = Staff::update($data,$where);
-
- dump($lma->getData());
-
-
- }
- // 添加时有默认年龄 【在里面传入的时候 是不能更改默认的值的】
- public function mn4()
- {
- $data=[
- 'name'=>'周小胖子',
- 'juese'=>'研发部',
- 'age'=>19 // 这句话是无效的,因为设置了所以不能更改
- ];
- $lma =Staff::create($data);
- dump($lma->getData());
- }
- // 这是更新时。默认更新掉一个值 atuo 没有单独试 这两个都会触发
- public function mn5()
- {
- $data=[
- 'name'=>'周巨胖子',
- 'gongzi'=>'3500' , // 这句话一样 也没啥卵用 更新时不给改
- ];
- $where=[
- 'id'=>35
- ];
- $lma =Staff::update($data,$where);
- dump($lma->getData());
- }
- }
复制代码
|
|