求助关联一对多的查询问题

浏览:540 发布日期:2018/05/28 分类:求助交流 关键字: 关联 jion
订单表,一对多查询(hasMany)得到数组组下:
(
[0] => Array
(
[id] => 1
[order_num] => 152688328655668
[shop_id] => 1
[price] => 118.0
[uid] => 1
[start] => 0
[des] => 骞磋交浜虹涓�鍙版墜鏈�
[youhui] =>
[ems] =>
[addtime] => 1526883286
[order_list] => Array
(
[0] => Array
(
[id] => 1
[order_id] => 1
[goods_id] => 1
[shop_id] => 3
[price] => 118
[chima] => 31
[num] => 1
[ems] => 0
[addtime] => 1526883286
)

)

)
那么问题来了,如何用join关联查到order_list下面的 比如goods 表的信息?
因为我存的都是表id,想后期直接用关联即可得到数句!

下面我贴上代码供大神解析:

控制品代码:
/**
* 订单列表
*/
public function index()
{
$order = new \app\shop\model\Order();
$list = $order->with('orderList')->paginate(20);
$list=collection($list)->toArray();
print_r($list);die;
// $this->assign('list',$list);
return $this->fetch();
}

模型a表代码:
namespace app\shop\model;
use think\Model;

class Order extends Model
{
protected $name="order";
public function orderList(){
return $this->hasMany("OrderList",'order_id',"id");
}

}
模型b表代码:
class OrderList extends Model{
protected $name='order_list';

public function order(){
return $this->belongsTo('Order');
}

}
最佳答案
评论( 相关
后面还有条评论,点击查看>>