分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

Thinkphp5 关联模型

发布时间:2023-09-06 01:56责任编辑:沈小雨关键词:暂无标签

必须建立两个模型分类模型(attr)、文章模型(article) 

attr模型

<?phpnamespace app\common\model;use think\Model;class Attr extends Model{ ???}

article模型

1 <?php2 namespace app\common\model;3 use think\Model;4 5 class Article extends Model{6 7 }

hasOne

hasMany

 1 <?php 2 namespace app\common\model; 3 use think\Model; 4 ?5 class Attr extends Model{ 6 ????// 关联文章模型 7 ????public function article() 8 ????{ 9 ????????return $this->hasMany(‘article‘,‘pid‘,‘id‘);10 ????????//return $this->hasOne(‘article‘,‘pid‘);11 ????}12 ????// 可用13 ????public function s(){14 ????????$id = 9;15 ????????$attr = $this->get($id);16 ????????// 查询单条17 ????????//$res = $attr->article()->find();18 ????????// 查询多条19 ????????$res = $attr->article()->select();20 ????????return $res;21 22 ????}23 24 }

控制器调用

 1 <?php 2 namespace app\index\controller; 3 use think\Controller; 4 use think\Model; 5 class Index extends Controller 6 { 7 ????protected $model; 8 ????public function _initialize() 9 ????{10 ????????parent::_initialize(); // TODO: Change the autogenerated stub11 ????????$this->model = model(‘attr‘);12 ????}13 14 ????public function index(){15 ????????$d = $this->model->s();16 ????????print_r($d);17 ????}18 19 ???20 21 }

输出结果:只有article 内容(没有attr内容)。hasMany关联相当于Model(‘article‘)

Thinkphp5 关联模型

原文地址:https://www.cnblogs.com/wesky/p/9083020.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved