Blogger Information
Blog 37
fans 0
comment 0
visits 14451
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp 模型操作
秋闲独醉
Original
351 people have browsed it

thinkphp 模型操作

  1. namespace app\model;
  2. use think\Model;
  3. use think\facade\Request;
  4. class User extends Model
  5. {
  6. //设置表名,如果遵循命名规则,可不用设置
  7. // protected $table= 'think_user';
  8. //设置主键
  9. protected $pk = 'uid';
  10. // 设置当前模型的数据库连接
  11. // protected $connection = 'mysql_two';
  12. //设置表名,默认没有前后缀
  13. // protected $name = 'users';
  14. //废弃字段disuse
  15. protected $disuse = ['add_time','add_ip','last_ip','last_time','delete_time'];
  16. //允许写入的字段field
  17. protected $field = ['nickname','phone','status','password'];
  18. //设置字段信息
  19. // protected $schema = [];
  20. // 设置json类型字段
  21. protected $json = ['info'];
  22. //查询think_user表里的数据
  23. public function getAllUser()
  24. {
  25. $result = User::select()->toArray();
  26. return $result;
  27. }
  28. //查询think_user表里面uid=1的数据
  29. public function getSignUser($id)
  30. {
  31. $result = User::where('uid','=',$id)->find();
  32. $info = $result->getAttr('nickname');
  33. // echo '我是usermodel里面的'.$info."<br>";
  34. return $result;
  35. }
  36. //改变状态的返回类型get+字段名+Attr
  37. public function getStatusAttr($status)
  38. {
  39. $array = [0=>'关闭',1=>'开启'];
  40. return $array[$status];
  41. }
  42. //设置
  43. public function setPasswordAttr($value)
  44. {
  45. return md5($value);
  46. }
  47. //查找并更新
  48. public function myUpdate($id,$info)
  49. {
  50. //获取用户信息
  51. // $user = $this->getSignUser($id);
  52. // foreach($info as $key => $value)
  53. // {
  54. // $user->$key = $value;
  55. // }
  56. // if($user->save())
  57. // {
  58. // return '更新成功';
  59. // }else{
  60. // return '更新失败';
  61. // }
  62. //字段过滤
  63. // $user = User::find($id);
  64. // echo $user;
  65. // if($user->allowField(['nickname','phone','status'])->save($info))
  66. // {
  67. // return '更新成功';
  68. // }else{
  69. // return '更新失败';
  70. // }
  71. }
  72. //最佳用法是在传入模型数据之前就进行过滤
  73. public function updateUser($id)
  74. {
  75. $user = User::find($id);
  76. $data = Request::only(['nickname','phone','status']);
  77. if($user->save($data))
  78. {
  79. return '更新成功'.json_encode($data);
  80. }else{
  81. return '更新失败'.json_encode($data);
  82. }
  83. }
  84. //删除
  85. public function deleteUser($id)
  86. {
  87. //删除当前模型
  88. $user = User::find($id);
  89. if($user->delete())
  90. {
  91. return '删除成功';
  92. }else{
  93. return '删除失败';
  94. }
  95. // 根据主键删除
  96. // if(User::destroy(14))
  97. // {
  98. // return '删除成功'.json_encode(User::getlastSql());
  99. // }else{
  100. // return '删除失败'.json_encode(User::getlastSql());
  101. // }
  102. }
  103. //查询
  104. public function findUser()
  105. {
  106. // $user = User::where('nickname','钟老师')->find();
  107. // return $user->getAttr('nickname');
  108. //获取多个数据
  109. // $list = User::where('status',1)
  110. // ->limit(5)
  111. // ->order('uid','desc')
  112. // ->select();
  113. // return $list;
  114. //获取某个字段或者某个列的值
  115. $user = User::where('status',1)
  116. ->column(['nickname,phone','uid']);
  117. return $user;
  118. }
  119. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post