🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[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; } ```