在ThinkPHP中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,ThinkPHP很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个例子.让大家理解ThinkPHP关联模型的意思.
环境描述:公司有一个员工表think_user,一个档案表,think_archives,一个部门表,think_department,和一个银行卡表.think_cars.
一个员工只有一个档案表,所以关系就是HSA_ONE,
一个员工只属于一个部门,但是部门里有多个员工,所以是BELONGS_TO关系
一个员工有多个银行卡,但是一个银行卡只能属于一个员工.所以关系就是HAS_MANY.
先创建需要的表和测试数据
think_user表.
1
2
3
4
5
6
7
|
CREATE
TABLE
`think_user` (
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
`username`
varchar
(50)
NOT
NULL
,
`
password
`
varchar
(50)
NOT
NULL
,
`did`
int
(11)
NOT
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8 |
|
think_department
1
2
3
4
5
|
CREATE
TABLE
`think_department` (
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
`
name
`
varchar
(50)
DEFAULT
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8 |
|
think_archives
1
2
3
4
5
6
7
8
|
CREATE
TABLE
`think_archives` (
`id`
int
(7)
NOT
NULL
AUTO_INCREMENT,
`uid`
int
(11)
NOT
NULL
,
`addr`
varchar
(200)
DEFAULT
NULL
,
`email`
varchar
(30)
DEFAULT
NULL
,
`tel`
int
(13)
DEFAULT
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8 |
|
think_cars
1
2
3
4
5
6
7
|
CREATE
TABLE
`think_cars` (
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
`
name
`
varchar
(50)
DEFAULT
NULL
,
`type`
varchar
(50)
DEFAULT
NULL
,
`uid`
int
(11)
DEFAULT
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8
|
插入数据到部门表think_department
1
2
3
4
|
insert
into
think_cars
values
(
null
,
'gongxiang'
,
'工商卡'
,
'1'
),
(
null
,
'jianshe'
,
'建行卡'
,
'2'
),
(
null
,
'jiaohang'
,
'交通银行卡'
,3);
|
think_user数据
1
2
3
4
5
6
7
8
9
10
|
insert
into
think_archives
values
(
null
,1,
'北京'
,
'123@163.com'
,
'13888888'
),
(
null
,2,
'上海'
,
'111@qq.com'
,
'1377777'
),
(
null
,3,
'重庆'
,
'222@qq.com'
,
'1344444'
),
(
null
,4,
'天津'
,
'333@qq.com'
,
'1111111'
),
(
null
,5,
'山西'
,
'444@qq.com'
,
'1322222'
),
(
null
,6,
'河北'
,
'555@qq.com'
,
'1333333'
),
(
null
,7,
'广州'
,
'6666@qq.com'
,
'13232323'
),
(
null
,8,
'广东'
,
'7777@qq.com'
,
'121121212'
),
(
null
,9,
'深证'
,
'888@qq.com'
,
'1821212'
);
|
think_cars数据
1
2
3
4
5
|
insert
into
think_cars
values
(
null
,
'gongxiang'
,
'工商卡'
,
'1'
),
(
null
,
'jianshe'
,
'建行卡'
,
'2'
),
(
null
,
'jiaohang'
,
'交通银行卡'
,3);
Query OK, 3
rows
affected (0.01 sec)
|
think_archives
1
2
3
4
5
6
7
8
9
10
|
insert
into
think_archives
values
(
null
,1,
'北京'
,
'123@163.com'
,
'13888888'
),
(
null
,2,
'上海'
,
'111@qq.com'
,
'1377777'
),
(
null
,3,
'重庆'
,
'222@qq.com'
,
'1344444'
),
(
null
,4,
'天津'
,
'333@qq.com'
,
'1111111'
),
(
null
,5,
'山西'
,
'444@qq.com'
,
'1322222'
),
(
null
,6,
'河北'
,
'555@qq.com'
,
'1333333'
),
(
null
,7,
'广州'
,
'6666@qq.com'
,
'13232323'
),
(
null
,8,
'广东'
,
'7777@qq.com'
,
'121121212'
),
(
null
,9,
'深证'
,
'888@qq.com'
,
'1821212'
);
|
ok,数据和表创建完了,下面讲如何用ThinkPHP的关联模型去获取表中数据
现在Model文件夹里创建UserModel.class.php
1
2
|
<?php
class
UserModel extend RelationModel{}
|
-
先做部门和员工之间的关系.员工表的did和部门表的id对应
1
2
3
4
5
6
7
8
9
10
|
class
UserModel
extends
RelationModel{
protected
$_link
=
array
(
'Department'
=>
array
(
'mapping_type'
=>BELONGS_TO,
'class_name'
=>
'Department'
,
'mapping_name'
=>
'Department'
,
'foreign_key'
=>
'did'
,
),
);
}
|
mapping_type是要关联的模型类名
mapping_name 关联表的模型名称
foreign_key 关联表的外键定义
mapping_fields 关联表要查询的字段,默认为全部字段
condition 关联条件
parent_key 自引用关联字段
as_fields 字段别名定义
2.员工表和档案表之间的关系
1
2
3
4
5
6
7
8
|
protected
$_link
=
array
(
'Archives'
=>
array
(
'mapping_type'
=>HAS_ONE,
'class_name'
=>
'Archives'
,
'foreign_key'
=>
'id'
,
'condition'
=>
'uid'
),
);
|
3.员工表与银行卡表之间关系的定义
1
2
3
4
5
6
7
8
|
protected
$_link
=
array
(
'Cars'
=>
array
(
'mapping_type'
=>HAS_MANY,
'class_name'
=>
'Cars'
,
'foreign_key'
=>
'id'
,
'condition'
=>
'uid'
,
),
);
|
使用方法,在IndexAction.class.php中
1
2
3
4
5
6
7
|
class
IndexAction
extends
Action {
public
function
index(){
$user
=D(
'User'
);
$row
=
$user
->relation(true)->select();
dump(
$row
);
}
}
|