5.1 多对多关联模型报错!按照文档的方法不行,无法获取关联表的数据

浏览:478 发布日期:2018/01/06 分类:ThinkPHP5专区
admin.php 控制器$where['uid'] =1;
  $data = model('admin')->get($where);
 print_r($data);
打印结果:app\admin\model\Admin Object
(
    [data] => Array
        (
            [uid] => 1
            [username] => admin
            [password] => c7122a1349c22cb3c009da3613d242ab
            [passsalt] => hGGPLRi1
            [email] => 
            [city_id] => 
            [mobile] => 
            [create_time] => 
            [create_ip] => 
            [last_time] => 1515211663
            [last_ip] => 127.0.0.1
            [status] => 0
        )

    [relation] => Array
        (
        )

)
admin.php 模型// 管理员表
class Admin extends Model
{
  // protected $autoCheckFields = true;
   
  public function roles()
    { 
       // print_r($this->id);
       return $this->belongsToMany('AdminRole','admin_role_access','role_id','uid'); //belongsToMany('关联模型','中间表','外键','关联键');
    }
AdminRole 角色 模型
<?php
namespace app\admin\model;
use think\Model;

class AdminRole extends Model
{

   protected $autoCheckFields = true; 
   public function admin()
      {
          return $this->belongsToMany('admin','admin_role_access','uid','role_id');
      } 

附件 123.png ( 12.08 KB 下载:6 次 )

最佳答案
评论( 相关
后面还有条评论,点击查看>>