* 定义多对多关联
* @return \think\model\relation\BelongsToMany
*/
public function group()
{
//belongsToMany('关联模型','中间表','外键','关联键');
return $this->belongsToMany('Group', 'auth_group_access','group_id','uid');
}
$user = UserModel::get(1);
// 获取用户的所有角色
dump($user->group);
打印出来的数据是这样的
array(3) {
[0] => ob
["name":protected] => string(10) "auth_group"
["connection":protected] => array(0) {
}
["parent":protected] => NULL
["query":protected] => NULL
["table":protected] => NULL
["class":protected] => string(20) "app\demo\model\Group"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => NULL
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(4) {
["id"] => int(2)
["ti
["status"] => int(1)
["rules"] => string(113) "7,28,29,30,31,18,32,8,37,10,38,1,12,13,14,15,16,17,19,20,21,22,3,23,4,36,61,64,62,25,26,5,27,6,24,33,34,35,2,11,9"
}
["origin":protected] => array(6) {
["id"] => int(2)
["ti
["status"] => int(1)
["rules"] => string(113) "7,28,29,30,31,18,32,8,37,10,38,1,12,13,14,15,16,17,19,20,21,22,3,23,4,36,61,64,62,25,26,5,27,6,24,33,34,35,2,11,9"
["pivot__uid"] => int(1)
["pivot__group_id"] => int(2)
}
["relation":protected] => array(1) {
["pivot"] => ob
["parent"] => ob
["name":protected] => string(4) "user"
["connection":protected] => array(0) {
}
["parent":protected] => NULL
["query":protected] => NULL
["table":protected] => NULL
["class":protected] => string(19) "app\demo\model\User"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => string(2) "id"
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(16) {
["id"] => int(1)
["account"] => string(5) "admin"
["nickname"] => string(6) "王明"
["password"] => string(32) "fcea920f7412b5da7be0cf42b8c93759"
["bind_account"] => string(0) ""
["verify"] => string(1) "0"
["email"] => string(16) "200787160@qq.com"
["phone"] => string(11) "15386079612"
["remark"] => string(9) "管理员"
["create_time"] => string(19) "2015-08-01 10:27:30"
["update_time"] => string(19) "2017-11-24 12:35:01"
["status"] => int(1)
["type_id"] => int(0)
["addip"] => string(10) "127.0.0.12"
["isphone"] => NULL
["isemail"] => NULL
}
["origin":protected] => array(16) {
["id"] => int(1)
["account"] => string(5) "admin"
["nickname"] => string(6) "王明"
["password"] => string(32) "fcea920f7412b5da7be0cf42b8c93759"
["bind_account"] => string(0) ""
["verify"] => string(1) "0"
["email"] => string(16) "200787160@qq.com"
["phone"] => string(11) "15386079612"
["remark"] => string(9) "管理员"
["create_time"] => string(19) "2015-08-01 10:27:30"
["update_time"] => string(19) "2017-11-24 12:35:01"
["status"] => int(1)
["type_id"] => int(0)
["addip"] => string(10) "127.0.0.12"
["isphone"] => NULL
["isemail"] => NULL
}
["relation":protected] => array(1) {
["group"] => *RECURSION*
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["autoWriteTimestamp":protected] => bool(false)
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(true)
["updateWhere":protected] => array(1) {
["id"] => int(1)
}
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
["autoWriteTimestamp":protected] => bool(false)
["connection":protected] => array(0) {
}
["query":protected] => NULL
["name":protected] => string(17) "auth_group_access"
["table":protected] => NULL
["class":protected] => string(17) "auth_group_access"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => NULL
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(2) {
["uid"] => int(1)
["group_id"] => int(2)
}
["origin":protected] => array(2) {
["uid"] => int(1)
["group_id"] => int(2)
}
["relation":protected] => array(0) {
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(false)
["updateWhere":protected] => NULL
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["autoWriteTimestamp":protected] => bool(false)
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(true)
["updateWhere":protected] => NULL
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
[1] => ob
["name":protected] => string(10) "auth_group"
["connection":protected] => array(0) {
}
["parent":protected] => NULL
["query":protected] => NULL
["table":protected] => NULL
["class":protected] => string(20) "app\demo\model\Group"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => NULL
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(4) {
["id"] => int(3)
["ti
["status"] => int(1)
["rules"] => string(11) "28,29,30,31"
}
["origin":protected] => array(6) {
["id"] => int(3)
["ti
["status"] => int(1)
["rules"] => string(11) "28,29,30,31"
["pivot__uid"] => int(1)
["pivot__group_id"] => int(3)
}
["relation":protected] => array(1) {
["pivot"] => ob
["parent"] => ob
["name":protected] => string(4) "user"
["connection":protected] => array(0) {
}
["parent":protected] => NULL
["query":protected] => NULL
["table":protected] => NULL
["class":protected] => string(19) "app\demo\model\User"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => string(2) "id"
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(16) {
["id"] => int(1)
["account"] => string(5) "admin"
["nickname"] => string(6) "王明"
["password"] => string(32) "fcea920f7412b5da7be0cf42b8c93759"
["bind_account"] => string(0) ""
["verify"] => string(1) "0"
["email"] => string(16) "200787160@qq.com"
["phone"] => string(11) "15386079612"
["remark"] => string(9) "管理员"
["create_time"] => string(19) "2015-08-01 10:27:30"
["update_time"] => string(19) "2017-11-24 12:35:01"
["status"] => int(1)
["type_id"] => int(0)
["addip"] => string(10) "127.0.0.12"
["isphone"] => NULL
["isemail"] => NULL
}
["origin":protected] => array(16) {
["id"] => int(1)
["account"] => string(5) "admin"
["nickname"] => string(6) "王明"
["password"] => string(32) "fcea920f7412b5da7be0cf42b8c93759"
["bind_account"] => string(0) ""
["verify"] => string(1) "0"
["email"] => string(16) "200787160@qq.com"
["phone"] => string(11) "15386079612"
["remark"] => string(9) "管理员"
["create_time"] => string(19) "2015-08-01 10:27:30"
["update_time"] => string(19) "2017-11-24 12:35:01"
["status"] => int(1)
["type_id"] => int(0)
["addip"] => string(10) "127.0.0.12"
["isphone"] => NULL
["isemail"] => NULL
}
["relation":protected] => array(1) {
["group"] => *RECURSION*
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["autoWriteTimestamp":protected] => bool(false)
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(true)
["updateWhere":protected] => array(1) {
["id"] => int(1)
}
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
["autoWriteTimestamp":protected] => bool(false)
["connection":protected] => array(0) {
}
["query":protected] => NULL
["name":protected] => string(17) "auth_group_access"
["table":protected] => NULL
["class":protected] => string(17) "auth_group_access"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => NULL
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(2) {
["uid"] => int(1)
["group_id"] => int(3)
}
["origin":protected] => array(2) {
["uid"] => int(1)
["group_id"] => int(3)
}
["relation":protected] => array(0) {
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(false)
["updateWhere":protected] => NULL
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["autoWriteTimestamp":protected] => bool(false)
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(true)
["updateWhere":protected] => NULL
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
[2] => ob
["name":protected] => string(10) "auth_group"
["connection":protected] => array(0) {
}
["parent":protected] => NULL
["query":protected] => NULL
["table":protected] => NULL
["class":protected] => string(20) "app\demo\model\Group"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => NULL
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(4) {
["id"] => int(5)
["ti
["status"] => int(1)
["rules"] => string(29) "28,30,29,31,32,37,38,12,13,14"
}
["origin":protected] => array(6) {
["id"] => int(5)
["ti
["status"] => int(1)
["rules"] => string(29) "28,30,29,31,32,37,38,12,13,14"
["pivot__uid"] => int(1)
["pivot__group_id"] => int(5)
}
["relation":protected] => array(1) {
["pivot"] => ob
["parent"] => ob
["name":protected] => string(4) "user"
["connection":protected] => array(0) {
}
["parent":protected] => NULL
["query":protected] => NULL
["table":protected] => NULL
["class":protected] => string(19) "app\demo\model\User"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => string(2) "id"
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(16) {
["id"] => int(1)
["account"] => string(5) "admin"
["nickname"] => string(6) "王明"
["password"] => string(32) "fcea920f7412b5da7be0cf42b8c93759"
["bind_account"] => string(0) ""
["verify"] => string(1) "0"
["email"] => string(16) "200787160@qq.com"
["phone"] => string(11) "15386079612"
["remark"] => string(9) "管理员"
["create_time"] => string(19) "2015-08-01 10:27:30"
["update_time"] => string(19) "2017-11-24 12:35:01"
["status"] => int(1)
["type_id"] => int(0)
["addip"] => string(10) "127.0.0.12"
["isphone"] => NULL
["isemail"] => NULL
}
["origin":protected] => array(16) {
["id"] => int(1)
["account"] => string(5) "admin"
["nickname"] => string(6) "王明"
["password"] => string(32) "fcea920f7412b5da7be0cf42b8c93759"
["bind_account"] => string(0) ""
["verify"] => string(1) "0"
["email"] => string(16) "200787160@qq.com"
["phone"] => string(11) "15386079612"
["remark"] => string(9) "管理员"
["create_time"] => string(19) "2015-08-01 10:27:30"
["update_time"] => string(19) "2017-11-24 12:35:01"
["status"] => int(1)
["type_id"] => int(0)
["addip"] => string(10) "127.0.0.12"
["isphone"] => NULL
["isemail"] => NULL
}
["relation":protected] => array(1) {
["group"] => *RECURSION*
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["autoWriteTimestamp":protected] => bool(false)
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(true)
["updateWhere":protected] => array(1) {
["id"] => int(1)
}
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
["autoWriteTimestamp":protected] => bool(false)
["connection":protected] => array(0) {
}
["query":protected] => NULL
["name":protected] => string(17) "auth_group_access"
["table":protected] => NULL
["class":protected] => string(17) "auth_group_access"
["error":protected] => NULL
["validate":protected] => NULL
["pk":protected] => NULL
["field":protected] => array(0) {
}
["except":protected] => array(0) {
}
["disuse":protected] => array(0) {
}
["readonly":protected] => array(0) {
}
["visible":protected] => array(0) {
}
["hidden":protected] => array(0) {
}
["append":protected] => array(0) {
}
["data":protected] => array(2) {
["uid"] => int(1)
["group_id"] => int(5)
}
["origin":protected] => array(2) {
["uid"] => int(1)
["group_id"] => int(5)
}
["relation":protected] => array(0) {
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(false)
["updateWhere":protected] => NULL
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
}
["auto":protected] => array(0) {
}
["insert":protected] => array(0) {
}
["update":protected] => array(0) {
}
["autoWriteTimestamp":protected] => bool(false)
["createTime":protected] => string(11) "create_time"
["updateTime":protected] => string(11) "update_time"
["dateFormat":protected] => string(11) "Y-m-d H:i:s"
["type":protected] => array(0) {
}
["isUpdate":protected] => bool(true)
["updateWhere":protected] => NULL
["failException":protected] => bool(false)
["useGlobalScope":protected] => bool(true)
["batchValidate":protected] => bool(false)
["resultSetType":protected] => string(5) "array"
["relationWrite":protected] => NULL
}
}
那这有什么用呢 还不如自已写原生SQL
TP3.2的时候都比这个好用
而且模型的文档也很少
<掌握ThinkPHP5.0数据库和模型>
里面也没有详细说明关联模型的用法
最佳答案