作为新手,这几天一直在研究老猫的thinkcmf5,碰到一个问题,想提出来请大牛指点一下。
cmf_user这个表设置了一个一对一关联的表(cmf_user_info), user_info表为用户的扩展属性表,如每个用户的实名realname,微信,QQ号等信息。
UserModel中新增:
public function userInfo() {
return $this->hasOne('UserInfoModel', 'user_id', 'id');
}
UserInfoModel中新增:
public function user() {
return $this->belongsTo('UserModel', 'id', 'user_id');
}
新建了一TestController.php文件,其中index()方法参考了老猫原IndexController中:
$id = $this->request->param("id", 0, "intval");
$userQuery = Db::name("User");
//$user = $userQuery->where('id',$id)->find();
$user = $userQuery->where('id',$id)->with(user_info')->find();
echo '实名:' . $user->user_info->realname . "<br/>";
测试时出现下面错误:
Fatal error: Class '' not found in D:\phpStudy\WWW\thinkcmf\simplewind\thinkphp\library\think\db\Query.php on line 1899
[1] ErrorException in Query.php line 1899
Class '' not found
if (is_string($with)) {
$with = explode(',', $with);
}
$first = true;
$currentModel = $this->model;
/** @var Model $class */
$class = new $currentModel;
foreach ($with as $key => $relation) {
$subRelation = '';
$closure = false;
if ($relation instanceof \Closure) {
// 支持闭包查询过滤关联条件
$closure = $relation;
$relation = $key;
$with[$key] = $key;
} elseif (is_array($relation)) {
老猫说问题是model和db不能混用,model中才能建立关联,应该用UserModel,那么应该怎样来建立这个查询并输出两个表中的字段呢?
0.话题发布后没及时显示,请联系官方QQ群管理;
1.话题支持代码,qq截屏直接粘贴,和QQ群一样;
2.问题描述清,比如服务器版本,程序版本;
3.能上代码,就不用文字;
4.把问题发到话题后再发QQ群;
5.如果感觉回复者解答的不错,可以发红包!
6.多帮助别人也是对自己的锻炼!
7.付费讨论中只有提问者和付费用户才可以查看回复
1.评论积分+1;每天最多10次奖励
2.发布话题积分+1;每天最多5次奖励
3.垃圾评论积分-500;
4.黄色,暴力,违反我国法律评论直接封号