模型
namespace app\common\model;
use think\Model;
class Order extends Model
{
public function goods(){
return $this->belongsTo(‘goods’,‘gid’,‘id’)->bind(‘goodsname’);
}
}
控制器
d
a
t
a
=
m
o
d
e
l
(
′
o
r
d
e
r
′
)
−
>
w
i
t
h
(
′
g
o
o
d
s
′
)
−
>
f
i
e
l
d
(
′
a
d
d
t
i
m
e
,
t
o
t
a
l
p
r
i
c
e
,
i
s
p
a
y
,
s
t
a
t
u
s
,
h
e
x
i
a
o
,
h
e
x
i
a
o
s
t
a
t
u
s
′
)
−
>
o
r
d
e
r
(
′
i
d
d
e
s
c
′
)
−
>
w
h
e
r
e
(
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
d
a
t
a
=
m
o
d
e
l
(
′
o
r
d
e
r
′
)
−
>
w
i
t
h
(
′
g
o
o
d
s
′
)
−
>
f
i
e
l
d
(
′
a
d
d
t
i
m
e
,
t
o
t
a
l
p
r
i
c
e
,
i
s
p
a
y
,
s
t
a
t
u
s
,
h
e
x
i
a
o
,
h
e
x
i
a
o
s
t
a
t
u
s
,
g
o
o
d
s
n
a
m
e
′
)
−
>
o
r
d
e
r
(
′
i
d
d
e
s
c
′
)
−
>
w
h
e
r
e
(
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();
d
a
t
a
=
m
o
d
e
l
(
′
o
r
d
e
r
′
)
−
>
w
i
t
h
(
′
g
o
o
d
s
′
)
−
>
f
i
e
l
d
(
′
g
i
d
,
a
d
d
t
i
m
e
,
t
o
t
a
l
p
r
i
c
e
,
i
s
p
a
y
,
s
t
a
t
u
s
,
h
e
x
i
a
o
,
h
e
x
i
a
o
s
t
a
t
u
s
,
g
o
o
d
s
n
a
m
e
′
)
−
>
o
r
d
e
r
(
′
i
d
d
e
s
c
′
)
−
>
w
h
e
r
e
(
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
正确的写法:
d
a
t
a
=
m
o
d
e
l
(
′
o
r
d
e
r
′
)
−
>
f
i
e
l
d
(
′
g
i
d
,
a
d
d
t
i
m
e
,
t
o
t
a
l
p
r
i
c
e
,
i
s
p
a
y
,
s
t
a
t
u
s
,
h
e
x
i
a
o
,
h
e
x
i
a
o
s
t
a
t
u
s
′
)
−
>
w
i
t
h
(
′
g
o
o
d
s
′
)
−
>
o
r
d
e
r
(
′
i
d
d
e
s
c
′
)
−
>
w
h
e
r
e
(
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)加进去,否则关联不上.