首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP
TP5.1模型关联写法
作者:小萝卜 2021-05-06 【 TP5 】 浏览 1684
简介
一对一关联,模型里面这么写
<?php
namespace appdemomodel;
use thinkDb;
use thinkModel;
use thinkmodelconcernSoftDelete;
class Attr extends Model
{
public function dyg()//dyg是我另一个模型,
{
return $this->hasOne('Dyg','Attr_id','id');//dyg这个表里面 要有Attr_id这个字段
}
}
<?php
namespace appdemocontroller;
use thinkDb;
use appdemomodelDyg; //,模型
use appdemomodelAttr; //,模型
use thinkRequest;
use thinkController;
use thinkdbWhere;
class Demo extends Controller
{
$user = Attr::get(5);
// 输出Dyg关联模型的user属性
echo $user->Dyg->user;
//也可以
echo $user->Dyg;
}
很赞哦! (0)
相关文章
- TP6利用Session实现前置中间件验证登录
- TP3.2使用join查询实例以及注意事项
- TP6定义常量报错:Use of undefined constant CONTROLLER_NAME - assumed 'CONTROLLER_NAME'
- TP6插入数据自动写入时间
- TP6上传图片验证时报错think\\Validate::check() must be of the type array
- tp5 上传文件时怎么让他不自动生成子文件夹?
- TP8使用注解+中间件实现在用户在后台某些特定操作的日志收集
- TP5 添加平铺文字水印,并设置文字之间的间距和文字的角度
- TP5 自定义验证器
- TP5/6验证器验证身份号(使用自定义验证规则)
