您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

ThinkPHP关联模型详解_MySQL

2024/4/2 4:59:47发布11次查看
thinkphp
        在thinkphp中,关联模型更类似一种mysql中的外键约束,但是外键约束更加安全,缺点却是在写sql语句的时候不方便,thinkphp很好得解决了这个问题.但是很多人不动关联模型的意思.现在就写个例子.让大家理解thinkphp关联模型的意思.
        环境描述:公司有一个员工表think_user,一个档案表,think_archives,一个部门表,think_department,和一个银行卡表.think_cars.
        一个员工只有一个档案表,所以关系就是hsa_one,
        一个员工只属于一个部门,但是部门里有多个员工,所以是belongs_to关系
        一个员工有多个银行卡,但是一个银行卡只能属于一个员工.所以关系就是has_many.
        先创建需要的表和测试数据
       think_user表.
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
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
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
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
insert into think_cars values (null,'gongxiang','工商卡','1'), (null,'jianshe','建行卡','2'), (null,'jiaohang','交通银行卡',3);
think_user数据
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数据
insert into think_cars values (null,'gongxiang','工商卡','1'),(null,'jianshe','建行卡','2'),(null,'jiaohang','交通银行卡',3);query ok, 3 rows affected (0.01 sec)
think_archives
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
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.员工表和档案表之间的关系
protected $_link=array( 'archives'=>array( 'mapping_type'=>has_one, 'class_name'=>'archives', 'foreign_key'=>'id', 'condition'=>'uid' ), );
3.员工表与银行卡表之间关系的定义
protected $_link=array( 'cars'=>array( 'mapping_type'=>has_many, 'class_name'=>'cars', 'foreign_key'=>'id', 'condition'=>'uid', ), );
使用方法,在indexaction.class.php中
class indexaction extends action { public function index(){ $user=d('user'); $row=$user->relation(true)->select(); dump($row); }}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product