ThinkPHP5中的模型一對多關聯,以及多張表關聯並輸出到同一個數組中,並附上實例。

首先我們上次講解什麼是一對的關聯,今天我們講解一對多的關聯,舉個例子。這裏的表我們知道沿用上次的表就行了,有些地方稍作小小的修改,假設我們現在有一個用戶表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模型的強大並非只是這個用法。那麼今天就先講到這裏,拜拜。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章