模型

namespace app\common\model;
use think\Model;
class Order extends Model
{
public function goods(){
return $this->belongsTo(‘goods’,‘gid’,‘id’)->bind(‘goodsname’);
}
}

控制器

data=model(′order′)−>with(′goods′)−>field(′addtime,totalprice,ispay,status,hexiao,hexiaostatus′)−>order(′iddesc′)−>where(data = model('order')->with('goods')->field('addtime,totalprice,ispay,status,hexiao,hexiaostatus')->order('id desc')->where(data=model(order)>with(goods)>field(addtime,totalprice,ispay,status,hexiao,hexiaostatus)>order(iddesc)>where(where)->select();
field这样写 查询的结果没有goodsname

data=model(′order′)−>with(′goods′)−>field(′addtime,totalprice,ispay,status,hexiao,hexiaostatus,goodsname′)−>order(′iddesc′)−>where(data = model('order')->with('goods')->field('addtime,totalprice,ispay,status,hexiao,hexiaostatus,goodsname')->order('id desc')->where(data=model(order)>with(goods)>field(addtime,totalprice,ispay,status,hexiao,hexiaostatus,goodsname)>order(iddesc)>where(where)->select();
data=model(′order′)−>with(′goods′)−>field(′gid,addtime,totalprice,ispay,status,hexiao,hexiaostatus,goodsname′)−>order(′iddesc′)−>where(data = model('order')->with('goods')->field('gid,addtime,totalprice,ispay,status,hexiao,hexiaostatus,goodsname')->order('id desc')->where(data=model(order)>with(goods)>field(gid,addtime,totalprice,ispay,status,hexiao,hexiaostatus,goodsname)>order(iddesc)>where(where)->select();
这两种field这样写 报错 Column not found: 1054 Unknown column ‘goodsname’ in ‘field list’ 查不到goodsname

正确的写法:
data=model(′order′)−>field(′gid,addtime,totalprice,ispay,status,hexiao,hexiaostatus′)−>with(′goods′)−>order(′iddesc′)−>where(data = model('order')->field('gid,addtime,totalprice,ispay,status,hexiao,hexiaostatus')->with('goods')->order('id desc')->where(data=model(order)>field(gid,addtime,totalprice,ispay,status,hexiao,hexiaostatus)>with(goods)>order(iddesc)>where(where)->select();
也就是说:field 应在with关联预载入之前调用,并且要把关联的条件字段(gid)加进去,否则关联不上.

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐