Blogger Information
Blog 62
fans 3
comment 1
visits 29898
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp联表查询与模型相关操作
kiraseo_wwwkiraercom
Original
404 people have browsed it

thinkphp联表查询与模型相关操作

thinkphp联表查询

代码如下

  1. echo '联表操作';
  2. $ret = Db::connect('serach')
  3. ->table('admin')
  4. ->alias('a')
  5. ->join(['admins_roles'=>'b'],'a.id= b.aid')
  6. ->select()
  7. ->toArray();
  8. echo '<pre>';
  9. print_r($ret);
  10. echo '模型操作查询与修改器的应用<br/>';
  11. $user = new user();
  12. $data = $user->cha()->toArray();
  13. print_r($data);

输出效果

模型操作(model创建 model查询数据库 model 的配置 model 获取器、修改器)

模型代码

  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model;
  4. use think\Model;
  5. // 2、model创建 3、model查询数据库 4、model 的配置
  6. /**
  7. * @mixin \think\Model
  8. */
  9. class user extends Model
  10. {
  11. // 设置当前模型的数据库连接
  12. protected $connection = 'dsp';
  13. //定义表名
  14. protected $name = 'kira_user';
  15. //定义主键
  16. protected $pk = 'uid';
  17. //状态修改
  18. public function getStatusAttr($value)
  19. {
  20. $status = [0=>'删除',1=>'正常'];
  21. return $status[$value];
  22. }
  23. // 设置字段信息
  24. protected $schema = [
  25. 'uid' => 'int',
  26. 'nickname' => 'string',
  27. 'password' => 'string',
  28. 'phone' =>'int',
  29. 'status' => 'int',
  30. ];
  31. //查询模型方法
  32. public function cha(){
  33. return user::find(7);
  34. }
  35. //添加模型方法
  36. public function add($data){
  37. return $this->create($data);
  38. }
  39. //密码进行处理
  40. public function setPasswordAttr($v,$cell)
  41. {
  42. return md5($v);
  43. }
  44. }

控制器代码

  1. public function tianjia(){
  2. echo "添加";
  3. $user = new user();
  4. $data= [
  5. 'nickname' => 'kira',
  6. 'password' => '123456',
  7. 'phone' => '18855556666'
  8. ];
  9. $user->add($data);
  10. }

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