首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP
TP5.1模型关联写法
作者:小萝卜 2021-05-06 【 TP5 】 浏览 2070
简介
一对一关联,模型里面这么写
<?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报错:Non-static method think\Request::controller() should not be called statically
- TP5 databackup拓展插件,实现数据库备份下载还原代码分享
- TP5实现微信H5支付
- TP6 databackup拓展插件,实现数据库备份下载还原代码分享
- ThinkPHP3.2和5.0的区别
- TP5/6验证器验证身份号(使用自定义验证规则)
- TP6/Mysql源生查询当前月每天的数据统计
- Swoole 结合TP5创建http服务
- TP6 TP5一键生成sitemap站点地图 代码分享
- TP5.1微信小程序支付功能服务器端代码分享
