首页
文章列表
交流群
文档
登录
fastadmin 一对一和一对多关联模型查询
2024-10-10 20:51:16
小程序码
文章目录
热门文章
整理下php学员遇到的一些问题
431
fastadmin监听或重写删除编辑按钮事件
285
phpstudy怎么手动安装php8.3.0版本?
275
文档:一个案例学会fastadmin插件开发
148
php基础入门课程资料文档课件
134
表单验证:密码和确认密码的验证和文字提示
76
百题斩:用递归方式扁平化嵌套数组
67
百题斩:字符串的逆序函数
60
案例代码:用油猴脚本统计b站课堂课程总时长
59
使用ffmpeg把mp4转为加密后的m3u8文件
55
### 视频地址 一对一查询:https://www.bilibili.com/video/BV1gL4y1p7w3/ 一对多查询:https://www.bilibili.com/video/BV1kq4y16789/ ### 一对一 文档地址:https://doc.fastadmin.net/doc/controller.html#toc-4 ```php hasOne('关联模型名','本表外键名','本表主键名',['模型别名定义'],'join类型'); belongsTo('关联模型名','本表外键名','关联表主键名',['模型别名定义'],'join类型'); ``` 样例:4个表 fa_stuclass fa_student fa_studentmoney fa_stuscore 主表为:fa_student 实现:操作学生主表,增删改查 学费表和成绩表,查询班级表 ### 关联查询 控制器里 ```php protected $relationSearch = true; /** * 快速搜索时执行查找的字段 */ protected $searchFields = 'id,admin.username'; ``` ### 关联写入(新增) ```php $this->model->stuscore()->save(["score"=>111,"student_id"=>$this->model->id]); $this->model->studentmoney()->save(["money"=>12,"student_id"=>$this->model->id]); ``` ### 关联修改 ```php $row->stuscore->score="xx"; $row->studentmoney->money="xxx"; $row->together(['stuscore','studentmoney'])->save(); 关联删除 $v->together(['stuscore','studentmoney'])->delete(); // 或者 $v->stuscore()->delete(); $v->studentmoney()->delete(); ``` ### 一对多 ```php hasMany('关联模型名','本表外键名','本表主键名',['模型别名定义']); ``` ### 关联统计 withCount() 也可在前端做统计 ```php $this->model->withCount('score')->where($where)->select(); ``` ### 关联查询 貌似是tp5.0的问题,没找到更好的办法 ```php $total = $this->model ->alias('student') ->with(['stuclass','stuscore']) ->join('stuscore','stuscore.student_id = student.id','LEFT') ->where($where) ->group('student.id') ->count(); $list = $this->model ->alias('student') ->with(['stuclass','stuscore']) ->join('stuscore','stuscore.student_id = student.id','LEFT') ->distinct(true) ->where($where) ->order($sort, $order) ->limit($offset, $limit) // ->fetchSql(true) ->select(); ``` 注意with和join同时用可能会有问题,可以改写为alisa join写法 ```php $total = $this->model ->alias('student') ->join('stuscore','stuscore.student_id = student.id','left') ->join('studentmoney','studentmoney.student_id = student.id','left') ->field('student.*') ->where($where) ->group('student.id') ->count(); $list = $this->model ->alias('student') ->join('stuscore','stuscore.student_id = student.id','left') ->join('studentmoney','studentmoney.student_id = student.id','left') ->field('student.*') ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); foreach ($list as $row) { $row->stuscore = model('stuscore')->where('student_id',$row->id)->select(); $row->studentmoney = model('studentmoney')->where('student_id',$row->id)->select(); } ``` ### 关联新增 ```php $this->model->stuscore()->saveAll([ ["score"=>111,"student_id"=>$this->model->id], ["score"=>333,"student_id"=>$this->model->id] ]); ``` ### 关联修改 感觉没有这个场景吧,together()貌似不支持,我也不会~ 如果真要批量改的话,就这样吧: ```php $this->model->stuscore()->where('student_id',$ids)->update(["score"=>"888"]); ``` ### 关联删除 ```php foreach ($list as $k => $v) { $count += $v->delete(); $v->stuscore()->delete(); } ```