在thinkphp中,關聯模型更類似一種mysql中的外來鍵約束,但是外來鍵約束更加安全,缺點卻是在寫sql語句的時候不方便,thinkphp很好得解決了這個問題.但是很多人不動關聯模型的意思.現在就寫個例子.讓大家理解thinkphp關聯模型的意思.
環境描述:公司有乙個員工表think_user,乙個檔案表,think_archives,乙個部門表,think_department,和乙個銀行卡表.think_cars.
乙個員工只有乙個檔案表,所以關係就是hsa_one,
乙個員工只屬於乙個部門,但是部門裡有多個員工,所以是belongs_to關係
乙個員工有多個銀行卡,但是乙個銀行卡只能屬於乙個員工.所以關係就是has_many.
先建立需要的表和測試資料
think_user表.
think_departmentcreate 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_archivescreate table `think_department` (
`id` int(11) not null auto_increment,
`name` varchar(50) default null,
primary key (`id`)
) engine=innodb default charset=utf8 |
think_carscreate 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_departmentcreate 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_user資料insert into think_cars values
(null,'gongxiang','工商卡','1'),
(null,'jianshe','建行卡','2'),
(null,'jiaohang','交通銀行卡',3);
think_cars資料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_archivesinsert into think_cars values
(null,'gongxiang','工商卡','1'),
(null,'jianshe','建行卡','2'),
(null,'jiaohang','交通銀行卡',3);
query ok, 3 rows affected (0.01 sec)
ok,資料和表建立完了,下面講如何用thinkphp的關聯模型去獲取表中資料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');
現在model資料夾裡建立usermodel.class.php
先做部門和員工之間的關係.員工表的did和部門表的id對應<?php
class usermodel extend relationmodel{}
class usermodel extends relationmodel
foreign_key 關聯表的外來鍵定義
condition 關聯條件
parent_key 自引用關聯字段
as_fields 字段別名定義
2.員工表和檔案表之間的關係
3.員工表與銀行卡表之間關係的定義protected $_link=array(
'archives'=>array(
'class_name'=>'archives',
'foreign_key'=>'id',
'condition'=>'uid'
),);
使用方法,在indexaction.class.php中protected $_link=array(
'cars'=>array(
'class_name'=>'cars',
'foreign_key'=>'id',
'condition'=>'uid',
),);
ThinkPHP關聯模型詳解
在thinkphp中,關聯模型更類似一種mysql中的外來鍵約束,但是外來鍵約束更加安全,缺點卻是在寫sql語句的時候不方便,thinkphp很好得解決了這個問題.但是很多人不動關聯模型的意思.現在就寫個例子.讓大家理解thinkphp關聯模型的意思.環境描述 公司有乙個員工表think user,...
thinkphp 模型關聯預載入和模型關聯統計
use think model class usermodel extends model public function getall public function getstatusattr value use think model class employeesmodel extends ...
關於thinkphp關聯模型的HAS ONE
距離第一次學習thinkphp有一年多了,也算是認識它和使用它一年多了吧,但是發現我對thinkphp還是有些不熟悉的地方,特別是模型。最近開發二手房專案,裡面用到模型,然後資料庫是沿用之前的,框架轉為thinkphp,之前一直自己設計資料庫,所以模型可以做的和官方一樣,但是這次因為資料庫不便於改動...