当前位置: 首页 > 后端技术 > PHP

tp模型关联使用

时间:2023-03-30 01:17:56 PHP

在数据库查询中,多表的join是不可避免的。tp为我们提供了模型关联,防止我们使用join进行关联。这里我以一对多为例:order表为主表,order_goods为关联表。定义订单模型hasMany(OrderGoods::class,'order_id','order_id')->field('goods_imageasimage');}}Defineorder_goodstablemodelgoodsList;}}订单表查询多条记录时publicfunctionorder(){//查询多条数据$list=OrderModel::where('id','>',1)->select();foreach($listas$item){//一对多关联查询$item->goodsList;}}其他关联方法的使用也类似。