先建立一個模型
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
);
}
}
|
可以看到你在數據表里的相關數據全部輸出了!