1. 关联模型问题
1.1 关联模型中,部分情况下with不能多层的闭包查询,需要用relation
// 推荐使用relation,在简单逻辑下可用with
$activity = Activity::with(['cover','banner'])->relation([
'stuffs'=>function($query){
$query->relation(['images'=>function($query){$query->limit(1);}]);
}
])->order('id DESC')->where('school_id','=',$school_id)->limit(1)->find();
1.2 使用filed的情况下,with中的外键一定要在filed中
public function user(){
return $this->belongsTo('Staff','user_id')
->field('id,name,avatar,department')->with('avatarPic,departmentInfo');
}
public function DepartmentInfo(){
return $this->belongsTo('Department','department');
}
1.3 多对多关系模型
//需要根据中间表中其他的字段排序的,可以使用如下方式:
//两个关系的中间表中有双方id和order排序字段
public function stuffs(){
return $this->belongsToMany('Stuff','ActivityStuff')->order('pivot.order ASC');
}
public function stuffsLimit(){
return $this->belongsToMany('Stuff','ActivityStuff')->order('pivot.order ASC')->limit(4);
}
2. Controller命名
Controller中前置方法一定不能驼峰,如果方法名是驼峰,直接在前置方法的方法参数表中用小写的方法名即可
//方法名分别是getExamListMP,getExamReport.......
protected $beforeActionList = [
'checkPrimaryToken'=>['only'=>'getexamlistmp,getexamdetail,uploadreport,gethistory'],
'checkAdminToken'=>['only'=>'getexamreport,deleteexam,createexam,updateexamquestion,updateexam,getexamlist']
];