1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- /**
- *
- *User:Administrator
- *Date:2021/10/19
- */
- namespace app\api\model;
- use think\Model;
- class User extends Model
- {
- //一个用户可以有多个评论,在User模型中是一对多的关系
- function comment()
- {
- return $this->hasMany('comment', 'user_id', 'id');
- }
- //一个用户只有唯一的简介,在User模型中是一对一的关系
- function profile()
- {
- return $this->hasOne('profile', 'user_id', 'id');
- }
- //三张表在同一级显示:user、profile、comment
- public function UserData1()
- {
- $user = new User();
- $data = $user->with(['profile', 'comment',])->find();
- return $data;
- }
- //三张表分级显示
- public function UserData2()
- {
- $user = new User();
- $data = $user->with([
- 'profile' => function ($query) {
- $query->with([
- 'comment' => function ($query) {
- }
- ]);
- }
- ])->select();
- return $data;
- }
- }
|