Build a model first
123456789101112 |
<?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"
)
);
}
?>
|
Then create the database. Create three tables respectively: Think_user think_group Think_user_group
User table UserID, username field
Group table GroupID, email fields
User_group table uid, GID field
The UID in the Foreign_key is associated with the UserID field in the user table
The GID in Relation_foreign_key is associated with the GroupID field in group
And then create it in the controller.
12345678910 |
<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction
extends Action {
public function index(){
$db
=D(
"user"
);
$list
=
$db
->relation(true)->select();
echo "<pre>"
;
print_r(
$list
);
}
}
|
You can see all the relevant data in the data table is output!