企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
网站一般都有banner轮播图,我们该怎么建表呢 一个banner_item就代表banner中的其中一个图,也就是banner_item **banner** | 字段 | 说明 | | --- | --- | | id | | | name | | | description | | | del_time | | | update_time | | ![](https://img.kancloud.cn/35/3c/353c18fa45c51798a0e57ac6534e6347_322x51.png) **banner_item** | 字段 | 说明 | | --- | --- | | id | | | img_id | | | key_word | | | del_time | | | banner_id | | | update_time | | ![](https://img.kancloud.cn/a4/4a/a44a05c1d8a7badd5cfdc18e33e3f045_408x84.png) **image** | 字段 | 说明 | | --- | --- | | id | | | url| | | from| | | del_time | | | update_time | | ![](https://img.kancloud.cn/6b/60/6b60f0ba2dc4b9f247b63e2e88643eef_323x567.png) 我们在banner控制器查询的时候使用with会将关联数据一起查询出来 直接查询:$banner = BannerModel::find($id) ![](https://img.kancloud.cn/ee/84/ee842970ea2bcd3a35f7e48ccf2700f2_607x219.png) 关联查询: $banner = BannerModel::with('items')->find($id) ![](https://img.kancloud.cn/28/dd/28dd61a40af36dec9c09acce62e977f8_642x408.png) 嵌套关联查询: $banner = BannerModel::with('items','items.img')->find($id) ![](https://img.kancloud.cn/6f/71/6f71fc737da38345931917fcf94e430d_591x409.png) 所以我们进一步优化封装代码 banner控制器 ``` namespace app\api\controller\v1; use app\api\controller\BaseController; use app\api\validate\IDMustBePositiveInt; use app\api\model\Banner as BannerModel; use app\lib\exception\MissException; /** * Banner资源 */ class Banner extends BaseController { // protected $beforeActionList = [ // 'checkPrimaryScope' => ['only' => 'getBanner'] // ]; /** * 获取Banner信息 * @url /banner/:id * @http get * @param int $id banner id * @return array of banner item , code 200 * @throws MissException */ public function getBanner($id) { $validate = new IDMustBePositiveInt(); $validate->goCheck(); $banner = BannerModel::getBannerById($id); if (!$banner ) { throw new MissException([ 'msg' => '请求banner不存在', 'errorCode' => 40000 ]); } return $banner; } } ``` Banner模型 ``` namespace app\api\model; use think\Model; class Banner extends BaseModel { //方法名可以随意,但是with参数必须和此方法名一致,并且,此方法名也是返回数据的数组的键名 public function items() { return $this->hasMany('BannerItem', 'banner_id', 'id'); } // /** * @param $id int banner所在位置 * @return Banner */ public static function getBannerById($id) { $banner = self::with(['items','items.img']) ->find($id); // $banner = BannerModel::relation('items,items.img') // ->find($id); return $banner; } } ``` BannerItem模型 ``` namespace app\api\model; use think\Model; class BannerItem extends BaseModel { protected $hidden = ['id', 'img_id', 'banner_id', 'delete_time']; public function img() { return $this->belongsTo('Image', 'img_id', 'id'); } // } ``` **Image ** ``` namespace app\api\model; use think\Model; class Image extends BaseModel { protected $hidden = ['delete_time', 'id', 'from']; public function getUrlAttr($value, $data) { return $this->prefixImgUrl($value, $data); } } ``` **BaseModel** ``` namespace app\api\model; use think\Model; use traits\model\SoftDelete; class BaseModel extends Model { // 软删除,设置后在查询时要特别注意whereOr // 使用whereOr会将设置了软删除的记录也查询出来 // 可以对比下SQL语句,看看whereOr的SQL use SoftDelete; protected $hidden = ['delete_time']; protected function prefixImgUrl($value, $data){ $finalUrl = $value; if($data['from'] == 1){ $finalUrl = config('setting.img_prefix').$value; } return $finalUrl; } } ```