TP5.1模型关联

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

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

}    
原文地址:https://www.cnblogs.com/79524795-Tian/p/14676828.html