标签:style blog http os 数据 io for cti
先建立一个模型
|
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); }} |
可以看到你在数据表里的相关数据全部输出了!
thinkphp 多对多关联模型(转),布布扣,bubuko.com
标签:style blog http os 数据 io for cti
原文地址:http://www.cnblogs.com/ada-zheng/p/3869588.html