thinkphp5、tp5中的多表关联查询[A表中有个外键包含b,b表中有个外键包含c,将abc表关联查询]三表查询
相信上述的例子经常会遇到!同样,如果在控制其中直接使用join链接查询的话,代码冗余!复用低
下面描述下tp5中model模型的实现方式
1:A[ziliao]表
class Ziliao extends Model
{
function User()
{
return $this->hasOne('Users','id','uid');
}
}
2:B(用户表)
hasOne('OrganisationInformation','id','ogid');
}
}
3:C【组织信息表】即这个用户属于哪个党委
4:model对应创建好了,我们来理里关系!
A表[ziliao]:储存了用户上传的学习资料,其中有个uid的字段,代表属于哪个用户
B表【user】:储存了用户的信息,其中有个ogid的字段代表它属于哪个组织
OrganisationInformation
C表[OrganisationInformation]储存了一些党委的信息,如地点,创建时间等信息
5:调用
关系理清了我们就来讲讲再控制器controller中的调用方式
$id = \request()->param("id");
$res = \app\common\model\Ziliao::with("user,user.OrganisationInformation")->find($id)->toArray();
p($res);
返回的接果如下
Array
(
[id] => 2
[title] => 学习邓小平精神
[uid] => 1402
[add_time] =>
[status] => 0
[imgs] =>
[ogid] => 7
[user] => Array
(
[id] => 1402
[username] => test
[idnumber] => 600230916665544455
[password] => 098f6bcd4621d373cade4e832627b4f6
[name] => 柯曾栎
[sex] => 1
[age] => 25
[birthday] =>
[native_place] =>
[nationality] => 汉族
[email] => 571031767@qq.com
[headimgurl] => /public/uploads/20180813/23ba7944052ebce55bf26cad307aeae3.jpg_small.png
[score] =>
[qualifications] => 博士
[reg_time] =>
[zhuanye] =>
[yubei_time] => 1970
[join_party_time] => -28800
[intrstime] => 1970
[introduce] =>
[where_now] =>
[address] => 西安
[logintime] => 0
[logouttime] => 0
[loginnum] => 0
[phone] => 15334555112
[ogid] => 7
[user_type] => 2
[job] => 软件工程师
[status] => 3
[lose] => 0
[losetime] =>
[liudong_status] => 0
[liuchufangxiang] =>
[is_excellent] => 0
[excellent_add_time] =>
[excellent_desc] =>
[dangneizhiwu] => 你猜猜
[jieshaoren] => ""
[organisation_information] => Array
(
[id] => 7
[pid] =>
[name] => 中共延长油田股份有限公司定边采油厂保卫科支部委员会
[img] =>
[type] => 631 党支部
[danwei] =>
[shuji] => 王军
[connect_user_name] => 王军
[phone] => 13335329888
[create_time] => 1970-01-01 08:00:00
[intro] =>
[address] => 定边县
[youbian] => 0
[other_info] =>
[ordid] => 255
)
)
)
好啦,教程到此结束!学会学不会不关本人的事了!喜欢收藏下吧!
本文出自 micuer.com转载请注明出处
http://micuer.com/index.php?m=index&a=read&id=315