多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
>[danger] hasOne() 一对一关联 ```php <?php namespace app\model; use think\Model; /** * 用户表模型 */ class User extends Model { /** * 关联用户资料表模型 */ public function profile() { // 用户资料表的uid 对应用户表的主键id return $this->hasOne(Profile::class, 'uid'); } } ``` >[danger] 模型查询 ``` // $id 用户id $data = self::find($id); // 没有关联到数据返回null // 关联到数据返回用户资料表模型数据对象 $profile = $data->profile; ``` ``` // $data 用户表数据集模型对象 $data = self::select(); foreach ($data as $value) { // $value 用户表数据模型对象 // $profile null或用户资料表模型对象 $profile = $value->profile; } ```