ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;banner(主表)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**banner_item(从表)**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img(主表) ![](https://img.kancloud.cn/e7/a9/e7a97a5794f54d4aad047f6d89a73b73_160x110.png) ![](https://img.kancloud.cn/85/ef/85ef771a4f89f6feb7f371f8bfd2b79f_199x111.png) ![](https://img.kancloud.cn/09/9f/099f640b69857b39d2d3aa0bc1eb7345_216x113.png) banner_item(从表) VS img(主表)        **一对一**     (belongsTo,它也是多对一)    这里用hasOne会报错 因为外键在banner\_item banneritem(从表) VS banner(主表) 相对关联     **多对一**     (从表中多个项同时包含一个主表的项) (belongsTo) ![](https://img.kancloud.cn/be/90/be90f07083ed7a7c7bcbac86b8d60030_465x98.png) >[info] **从表建立多对一关联关系** >belongsTo('关联主表模型名','从表外键名','关联表主表键名',\['模型别名定义'\],'join类型'); >返回belongsTo类对象 BannerItem.php Banner和Image两个为空模型,这里就不列出了 ``` <?php namespace app\index\model; use think\Model; use think\Db; class BannerItem extends Model{ public function itemRelationBanner(){ return $this->belongsTo('Banner','banner_id','id'); } public function image(){ return $this->belongsTo('img','img_id','id'); } } ``` ## **关联查询** ``` $a=\app\index\model\BannerItem::get(1); echo $a->itemRelationBanner;//或者$a->item_relation_banner;都行 echo $a->item_relation_banner->des; $res1=$a->toArray(); //不推荐动态获取关联表数据,它每次获取关联对象(如:$user->itemRelationBanner)的时候就会进行实际的关联查询,他会进行多次查询 //但可以使用预载入查询来提高查询性能,对于一对一关联来说,只需要进行一次查询即可获取关联对象数据 $b=\app\index\model\BannerItem::get(1,['itemRelationBanner', 'image']); $res2=$b->toArray(); $c=\app\index\model\BannerItem::with(['item_relation_banner', 'image'])->find(1); $res3=$c->toArray(); //查询多条 $d=\app\index\model\BannerItem::with(['item_relation_banner', 'image'])->select(); $d=\app\index\model\BannerItem::All([],['item_relation_banner', 'image']); $res4=collection($d)->toArray(); dump($res1); dump($res2); dump($res3); dump($res4); ``` ![](https://img.kancloud.cn/95/dc/95dc3df16c1861d5c9409f1c322a14e2_384x416.png) ![](https://img.kancloud.cn/97/4d/974d8f02b99212f09d59491df367e259_334x437.png) 只是查询当前模型查询关联模型的数据的话可以直接调用关联方法 ``` $a=\app\index\model\BannerItem::get(1); $res5=$a->itemRelationBanner()->select(); //相当于:"SELECT * FROM `banner` WHERE `id` = 1" $res5=$a->itemRelationBanner()->find(); //相当于:"SELECT * FROM `banner` WHERE `id` = 1 LIMIT 1" dump($res5->toArray()); ``` ![](https://img.kancloud.cn/64/12/64120d41ab861c28c18aae9e795d8800_222x100.png) ``` $a= new \app\index\model\BannerItem(); $res5=$a->itemRelationBanner()->select();//"SELECT * FROM `banner`" dump($res5->toArray()); ``` ![](https://img.kancloud.cn/0f/50/0f50671a4b7e171511fe61e0993b6fa4_169x190.png) 带条件查询 ``` $res5=\app\index\model\BannerItem::hasWhere('image',['path'=>'/public/img/3.jpg'])->where(['banner_id'=>1])->select(); sql: "SELECT `BannerItem`.* FROM `banner_item` `BannerItem` INNER JOIN `img` `Img` ON `BannerItem`.`img_id`=`Img`.`id` WHERE `Img`.`path` = '/public/img/3.jpg' AND `banner_id` = 1" ``` ## **关联新增**