// 建立关联
public function items()
{
// 一对一关系关联:belongsTo() 或 hasOne() ; 一对多关系关联: hasMany()
// 外键在主表中,使用belongsTo() , 目标在被关联表中,使用hasOne()
// 一对多关系 hasMany 方法 参数为('要关联的模型', '关联的id', 'id' )
return $this->hasMany('BannerItem', 'banner_id', 'id');
}
// protected $table = 'category'; // 查询其它表
public static function getBannerByID($id)
{
// 关联多个模型时, with()中填写数组 with(['items','items1']) 也可以嵌套使用
$banner = self::with(['items', 'items.img'])->find($id);
return $banner;
}
}
这种预加载有什么好处 当嵌套的时候就变的很难维护了
这种会影响性能吗
关联预载入 · ThinkPHP5.1完全开发手册