模型关联的方法手册上写到有,具体问题是查询的时候没有查出关联数据。
原来是查询的时候要使用with方法调用出模型里面的关联方法才行。
$u_list = UserModel::with('role')->where('is_admin','neq','1')->paginate(20);
模板调用数据方法
{volist name='u_list' id='vo'}
<tr>
<td>{$vo['uid']}</td>
<td>{$vo['mobile']}</td>
<td>{$vo['role']['role_name']}</td>
<td>{$vo['status']}</td>
<td>{$vo['create_time']}</td>
</tr>
{/volist}
<?php
namespace app\admin\common\model;
use think\Model;
class User extends Model
{
protected $pk = 'id';
protected $table = 'zh_user';
public function role(){
return $this->hasOne('Role','id','role_id');
}
}
<?php
namespace app\admin\common\model;
use think\Model;
class Role extends Model
{
protected $pk = 'id';
protected $table = 'zh_role';
public function user()
{
return $this->hasMany('User','role_id','id');
}
}