首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP

TP5.1模型关联写法

作者:小萝卜 2021-05-06 浏览 931

简介

一对一关联,模型里面这么写

<?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)

文章评论

    高端网站建设