tp6 模型关联查询

浏览:189 发布日期:2021/11/19 分类:ThinkPHP6专区 关键字: 模型绑定
public function getList($condition = [], $size = 10, $page = 1)
{
$count = $this->withJoin(["user", "coupon"])->where($condition)->count();
$data = $this->withJoin(["user", "coupon"])->where($condition)->order('order.id', 'DESC')->paginate($size);

return [
"count" => $count,
"data" => $data->items()
];
}

public function user()
{
return $this->hasOne(User::class, "uid", "uid")
->bind(["phone", "nickname"])->joinType("LEFT");
}

public function coupon()
{
return $this->hasOne(Coupon::class, "id", "goods_id")
->bind(["product_name"])->joinType("LEFT");
}


使用bind()绑定字段时,如果两个模型字段名相同要怎么处理呢,就像上面的代码里两个模型都有phone字段,这时绑定就报错,提示phone字段已存在
最佳答案
评论( 相关
后面还有条评论,点击查看>>