TP5 hasMany一对多关联查询

下面是一个查询用户登录历史的功能(简单的结构,为了解释一对多关联查询)

有2张表

表A:用户表(user)  user_id    user_name

表B:用户登录日志表(user_login_log)  user_id   IP

下面先附上一对多查询结果的数组样式

TP5 hasMany一对多关联查询_第1张图片

下面上代码(model层)

public function userLoginLog()
    {
        return $this->hasMany('UserLoginLog','user_id');
    }

content层

$list = collection(model('user')->with('userLoginLog')->where(['status'=>1])->select())->toArray();

 

你可能感兴趣的:(php,PHP,TP5,关联,查询,一对多)