当前位置:网站首页>Detailed explanation of ThinkPHP 5.0 Model Association
Detailed explanation of ThinkPHP 5.0 Model Association
2022-06-24 09:40:00 【BigChen_ up】
I believe many young friends are just beginning to learn thinkphp5.0 I can't understand the meaning of the parameters in the model association , Here is a detailed introduction :
One to one connection :hasOne(‘ Association model name ’,‘ Foreign key name ’,‘ Primary key name ’,[‘ Model alias definition ’],‘join type ’);
You can see a one-to-one relationship hasOne There are many parameters in , But the first three parameters are the most important .
The first parameter ‘ Association model name ’ ,tp5 This is what the manual says :‘ Every user has a profile ’.
To be more specific , You can use it in the user model hasOne Associated profile , So the first parameter is The model name of the profile . Look at the code :
namespace app\index\model;
use think\Model;
class User extends Model {
public function profile() {
return $this->hasOne('Profile');
}
}
The second parameter is ‘ Foreign key name ’ In fact, that is You need to associate Profile The primary key name of the table .
The third parameter is the primary key name , Is what your current model is associated with Profile Corresponding to table PK Field name .
public function profile() {
// The second parameter id yes Profile Tabular id, The third parameter pid It is required to follow the current model Profile The fields associated with the primary key of the table
return $this->hasOne('Profile','id','pid');
}
To use, you need to instantiate the model first Then query the data with -> To get the desired data :
$user = User::get(1);
// Output Profile Of the association model email attribute
echo $user->profile->email;
One to many associative The parameters are almost the same as one-to-one , Learn one-to-one association and other associations can also do basic operations .
边栏推荐
- Prct-1400: failed to execute getcrshome resolution
- ApplicationContextInitializer的三种使用方法
- Codeforces Round #392 (Div. 2) D. Ability To Convert
- 【bug】@JsonFormat 使用时出现日期少一天的问题
- 软件系统依赖关系分析
- Cmake命令之target_compile_options
- Algorithm - the K row with the weakest combat power in the matrix (kotlin)
- PRCT-1400 : 未能执行 getcrshome解决方法
- latex公式及表格识别
- LeetCode: 377. Combined sum IV
猜你喜欢

R ellipse random point generation and drawing

impdp导schema报ORA-31625异常处理

Grpc local test joint debugging tool bloomrpc

Support vector machine (SVC, nusvc, linearsvc)

NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读

ApplicationContextInitializer的三种使用方法

20、 Processor scheduling (RR time slice rotation, mlfq multi-level feedback queue, CFS fully fair scheduler, priority reversal; multiprocessor scheduling)

Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读

CDGA|到底怎么才能做好数据治理呢?

5 minutes, excellent customer service chat handling skills
随机推荐
Weekly recommended short video: talk about "meta universe" with a serious attitude
Cdga | how can we do well in data governance?
Grpc local test joint debugging tool bloomrpc
Project deployment related
20、 Processor scheduling (RR time slice rotation, mlfq multi-level feedback queue, CFS fully fair scheduler, priority reversal; multiprocessor scheduling)
如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!
Ora-28000 error after upgrading Oracle 12C to 19C
Longest public prefix of leetcode
如何规范化数据中心基础设施管理流程
蜜罐2款hfish,ehoney
How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
P6698-[balticoi 2020 day2] virus [AC automata, DP, SPFA]
牛客网 字符串变形
threejs的点光源+环境光
198. house raiding
threejs的 mmd模型加载+轮廓加载+动画加载+音频加载+相机动画加载+ammojs加载 gltf模型的加载 +gltf的反射调整
PTA monkey chooses King (Joseph Ring problem)
字节跳动-面试官: 谈下音视频同步原理,音频和视频能绝对同步吗?
达梦数据库如何定位锁等待问题解决方法
impdp导schema报ORA-31625异常处理