先建立一个模型
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
class
UserModel
extends
RelationModel{
protected
$_link
=
array
(
"group"
=>
array
(
"mapping_type"
=>MANY_TO_MANY,
"foreign_key"
=>
"uid"
,
//中间表的字段
"relation_foreign_key"
=>
"gid"
,
//中间表的字段
"relation_table"
=>
"think_user_group"
)
);
}
?>
|
然后创建数据库。分别创建三张表:think_user think_group think_user_group
user 表里userid、username字段
group 表里groupid、email字段
user_group 表里uid、gid字段
foreign_key 里的uid是与user表里的userid字段关联
relation_foreign_key 里的gid是与group里的groupid字段关联
然后在到控制器里创建
1
2
3
4
5
6
7
8
9
10
|
<?php
// 本类由系统自动生成,仅供测试用途
class
IndexAction
extends
Action {
public
function
index(){
$db
=D(
"user"
);
$list
=
$db
->relation(true)->select();
echo
"<pre>"
;
print_r(
$list
);
}
}
|
可以看到你在数据表里的相关数据全部输出了!