TP5实例模型中的一对多关联,以及多张表关联并输出到同一个数组中,并附上实例。

首先我们上次讲解什么是一对的关联,今天我们讲解一对多的关联,举个例子。这里的表我们知道沿用上次的表就行了,有些地方稍作小小的修改,假设我们现在有一个用户表member,有一个comment评论表。一个用户对应着多个评论对吧,可以是一条评论也可以是多条评论,这样这两张表之间就存在了一对多的关系了。下面我们开始构建这样的模型关系。

首先我们知道模型中告知我们,一对多的方法有hasMany()方法和belongsToMany(),那么我们先新建这两个模型,接着我们开始关联整个模型中的关系,代码如下:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/24 0024
 * Time: 00:10
 */

namespace app\index\model;


use think\Model;

class Member extends Model
{
    /**
     * @return \think\model\relation\HasOne
     * 这里是关联的一对关联模型,方法名为表明千万记住
     *
     */
    public function profile(){
        //hasOne()方法中第一个为关联的模型名称,第二个为关联的外键,
        //所以这里分别是Profile模型和profile_id外键
        return $this->hasOne('Profile','profile_id');
    }


    /**
     * @return \think\model\relation\HasMany
     * 这里是关联的一对多的情况,方法名同样为表明,记住了
     */

    public function comment(){

        return $this->hasMany('Comment','user_id');

    }


    /**
     * 下面是一对一关联的方法
     */

    public function Memberdata(){
        $Member=new Member();
        $data=$Member->with('profile')->field('id,username')->select();
        return $data ;
    }
    /**
     * 下面是3张表关联的方法,并且放在同一级下的方法
     */

    public function MemberList(){
        $Member=new Member();
        $data=$Member->with(['profile','comment'])->select();
        return $data;

    }

    /**
     * 下面是3张表,分别不放在同一级的关系下的情况,而是分层的情况下
     */

    public function MemberLists(){
        $Member=new Member();
        $data=$Member->with([
            'profile'=>function($query){
                $query->with([
                    'comment'=>function($query){

                    }
                ]);
            }
        ])->select();

        return $data;
    }

}

profile下面的模型代码,如下:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/24 0024
 * Time: 00:13
 */

namespace app\index\model;


use think\Model;

class Profile extends Model
{

    /**
     * 资料表一对多关联评论表,资料表可以理解为用户头像什么的,明白了。
     */

    public function comment(){

        return $this->hasMany('Comment','profile_id');

    }




}

然后这下面是控制层的代码,如下:

<?php
namespace app\index\controller;

use app\index\model\Member;
use think\Controller;

class Index extends Controller
{
    public function index()
    {

        $MemberData=model('Member');
        $data=$MemberData->Memberdata();
        return json($data);

    }

    public function comment(){
        $MemberData=model('Member');
        $data=$MemberData->MemberList();
        return json($data);
    }

    public function comments(){
        $MemberData=model('Member');
        $data=$MemberData->MemberLists();
        return json($data);
    }
}

然后我们看看分别输出的样式是什么样子的格式:

第一个index方法输入的样式如下,也就是一对一的关联部分,上次我们讲的地方:

[
  {
    "id": 1,
    "username": "abc",
    "profile": {
      "id": 1,
      "profile_id": 1,
      "profile_content": "abc资料内容"
    }
  },
  {
    "id": 2,
    "username": "efg",
    "profile": {
      "id": 2,
      "profile_id": 2,
      "profile_content": "efg资料内容"
    }
  },
  {
    "id": 3,
    "username": "hig",
    "profile": {
      "id": 3,
      "profile_id": 3,
      "profile_content": "hij资料内容"
    }
  },
  {
    "id": 4,
    "username": "klm",
    "profile": {
      "id": 4,
      "profile_id": 4,
      "profile_content": "klm资料内容"
    }
  }
]

第二个 comment方法,一对多关联的同级数据格式效果,如下:

[
  {
    "id": 1,
    "username": "abc",
    "password": "123456",
    "profile": {
      "id": 1,
      "profile_id": 1,
      "profile_content": "abc资料内容"
    },
    "comment": [
      {
        "id": 1,
        "user_id": 1,
        "profile_id": 1,
        "comment": "1用户评论的内容"
      },
      {
        "id": 2,
        "user_id": 1,
        "profile_id": 1,
        "comment": "1用户评论的内容"
      }
    ]
  },
  {
    "id": 2,
    "username": "efg",
    "password": "123456",
    "profile": {
      "id": 2,
      "profile_id": 2,
      "profile_content": "efg资料内容"
    },
    "comment": [
      {
        "id": 5,
        "user_id": 2,
        "profile_id": 2,
        "comment": "2用户评论内容"
      }
    ]
  },
  {
    "id": 3,
    "username": "hig",
    "password": "123456",
    "profile": {
      "id": 3,
      "profile_id": 3,
      "profile_content": "hij资料内容"
    },
    "comment": [
      {
        "id": 3,
        "user_id": 3,
        "profile_id": 3,
        "comment": "3用户评论的内容"
      }
    ]
  },
  {
    "id": 4,
    "username": "klm",
    "password": "123456",
    "profile": {
      "id": 4,
      "profile_id": 4,
      "profile_content": "klm资料内容"
    },
    "comment": [
      {
        "id": 4,
        "user_id": 4,
        "profile_id": 4,
        "comment": "4用户评论内容"
      }
    ]
  }
]

第三个方法comments,同样是一对多关联,但是分别是下表显示的格式的效果,如下:

[
  {
    "id": 1,
    "username": "abc",
    "password": "123456",
    "profile": {
      "id": 1,
      "profile_id": 1,
      "profile_content": "abc资料内容",
      "comment": [
        {
          "id": 1,
          "user_id": 1,
          "profile_id": 1,
          "comment": "1用户评论的内容"
        },
        {
          "id": 2,
          "user_id": 1,
          "profile_id": 1,
          "comment": "1用户评论的内容"
        }
      ]
    }
  },
  {
    "id": 2,
    "username": "efg",
    "password": "123456",
    "profile": {
      "id": 2,
      "profile_id": 2,
      "profile_content": "efg资料内容",
      "comment": [
        {
          "id": 5,
          "user_id": 2,
          "profile_id": 2,
          "comment": "2用户评论内容"
        }
      ]
    }
  },
  {
    "id": 3,
    "username": "hig",
    "password": "123456",
    "profile": {
      "id": 3,
      "profile_id": 3,
      "profile_content": "hij资料内容",
      "comment": [
        {
          "id": 3,
          "user_id": 3,
          "profile_id": 3,
          "comment": "3用户评论的内容"
        }
      ]
    }
  },
  {
    "id": 4,
    "username": "klm",
    "password": "123456",
    "profile": {
      "id": 4,
      "profile_id": 4,
      "profile_content": "klm资料内容",
      "comment": [
        {
          "id": 4,
          "user_id": 4,
          "profile_id": 4,
          "comment": "4用户评论内容"
        }
      ]
    }
  }
]

控制层中的方法comment方法和comments方法都同时实现了关联获取多张表的数据到一个数组的下标中,而且我们实际上并没有使用任何的foreach循环遍历数据,就已经获取到了我们想要的格式,是不是突然觉得模型的强大啦,当然TP5模型的强大并非只是这个用法。那么今天就先讲到这里,拜拜。

猜你喜欢

转载自blog.csdn.net/qq_39338006/article/details/82224535
今日推荐