User.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. *
  4. *User:Administrator
  5. *Date:2021/10/19
  6. */
  7. namespace app\api\model;
  8. use think\Model;
  9. class User extends Model
  10. {
  11. //一个用户可以有多个评论,在User模型中是一对多的关系
  12. function comment()
  13. {
  14. return $this->hasMany('comment', 'user_id', 'id');
  15. }
  16. //一个用户只有唯一的简介,在User模型中是一对一的关系
  17. function profile()
  18. {
  19. return $this->hasOne('profile', 'user_id', 'id');
  20. }
  21. //三张表在同一级显示:user、profile、comment
  22. public function UserData1()
  23. {
  24. $user = new User();
  25. $data = $user->with(['profile', 'comment',])->find();
  26. return $data;
  27. }
  28. //三张表分级显示
  29. public function UserData2()
  30. {
  31. $user = new User();
  32. $data = $user->with([
  33. 'profile' => function ($query) {
  34. $query->with([
  35. 'comment' => function ($query) {
  36. }
  37. ]);
  38. }
  39. ])->select();
  40. return $data;
  41. }
  42. }