9
版本:5.1
模型一对多关系,多的表with一的表,如果多的这边关联id为null然后find时会报错,看了一下出错的SQL为:
SELECT * FROM `ca_major_line` WHERE ( `id` = ) AND `ca_major_line`.`delete_time` IS NULL
id
没有拼进去,这里应该是IS NULL
才对
附上两个模型,还有查询代码:
/**
* Class Department
* @package app\common\model
* @property int id
* @property int comp_id
* @property int major_id
* @property string name
* @property int sort
* @property string delete_time
* @property string create_time
* @property string update_time
*/
class Department extends BaseModel {
public function majorLine() {
return $this->belongsTo('MajorLine', 'major_id', 'id');
}
}
/**
* Class MajorLine
* @package app\common\model
* @property int id
* @property string name
* @property string delete_time
* @property string create_time
* @property string update_time
*/
class MajorLine extends BaseModel {
public function department() {
return $this->hasMany('Department', 'major_id', 'id');
}
}
$dept = Department::with(['majorLine'])->where('id', 1)->find();