TP5.1模型关联

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

<?php
namespace app\demo\model;
use think\Db;

use think\Model;
use think\model\concern\SoftDelete;


class Attr extends Model
{

  public function dyg()//dyg是我另一个模型,
            {
                return $this->hasOne('Dyg','Attr_id','id');//dyg这个表里面 要有Attr_id这个字段  
            }

}

方法里面这么写:

<?php
namespace app\demo\controller;

use think\Db;
use app\demo\model\Dyg;  //,模型
use app\demo\model\Attr;  //,模型
use think\Request;
use think\Controller;
use think\db\Where;


class Demo extends Controller
{ 
         $user = Attr::get(5);
        // 输出Dyg关联模型的user属性
    echo $user->Dyg->user;
            //也可以      
          echo $user->Dyg;

}    

 

posted @ 2021-04-19 14:56  79524795  阅读(105)  评论(0)    收藏  举报