一對一關(guān)聯(lián)

 

版本 功能調(diào)整
5.0.5 增加關(guān)聯(lián)自動寫入和刪除
5.0.4 增加關(guān)聯(lián)屬性綁定到父模型功能

定義

定義一對一關(guān)聯(lián),例如,一個用戶都有一個個人資料,我們定義User模型如下:

namespace appindexmodel;

use thinkModel;

class User extends Model
{
    public function profile()
    {
        return $this->hasOne('Profile');
    }
}

hasOne方法的參數(shù)包括:

hasOne('關(guān)聯(lián)模型名','外鍵名','主鍵名',['模型別名定義'],'join類型');

默認(rèn)的join類型為INNER。

V5.0.3+版本開始,可以支持為關(guān)聯(lián)模型定義需要查詢的字段,例如:

namespace appindexmodel;

use thinkModel;

class User extends Model
{
    public function profile()
    {
        return $this->hasOne('Profile')->field('id,name,email');
    }
}

如果使用的是join方式的關(guān)聯(lián),不支持指定field字段。

5.0.5+版本開始,模型別名定義參數(shù)已經(jīng)廢棄。

關(guān)聯(lián)查找

定義好關(guān)聯(lián)之后,就可以使用下面的方法獲取關(guān)聯(lián)數(shù)據(jù):

$user = User::get(1);
// 輸出Profile關(guān)聯(lián)模型的email屬性
echo $user->profile->email;

如果要根據(jù)關(guān)聯(lián)表的查詢條件查詢當(dāng)前模型的數(shù)據(jù),可以使用hasWhere方法,例如:

$user = User::hasWhere('profile',['email'=>'thinkphp@qq.com'])->find();
echo $user->name;

默認(rèn)情況下, 我們使用的是user_id 作為外鍵關(guān)聯(lián),如果不是的話則需要在關(guān)聯(lián)定義的時候指定,例如:

<?php
namespace appindexmodel;

use thinkModel;

class User extends Model 
{
    public function profile()
    {
        return $this->hasOne('Profile','uid');
    }
}

有一點需要注意的是,關(guān)聯(lián)方法的命名規(guī)范是駝峰法,而關(guān)聯(lián)屬性則一般是小寫+下劃線的方式,系統(tǒng)在獲取的時候會自動轉(zhuǎn)換對應(yīng),讀取user_profile關(guān)聯(lián)屬性則對應(yīng)的關(guān)聯(lián)方法應(yīng)該是userProfile。

關(guān)聯(lián)新增

$user = User::get(1);
// 如果還沒有關(guān)聯(lián)數(shù)據(jù) 則進行新增
$user->profile()->save(['email' => 'thinkphp']);

系統(tǒng)會自動把當(dāng)前模型的主鍵傳入profile模型。

關(guān)聯(lián)更新

和新增一樣使用save方法進行更新關(guān)聯(lián)數(shù)據(jù)。

$user = User::get(1);
$user->profile->email = 'thinkphp';
$user->profile->save();
// 或者
$user->profile->save(['email' => 'thinkphp']);

定義相對的關(guān)聯(lián)

我們可以在Profile模型中定義一個相對的關(guān)聯(lián)關(guān)系,例如:

namespace appindexmodel;

use thinkModel;

class Profile extends Model 
{
    public function user()
    {
        return $this->belongsTo('User');
    }
}

belongsTo的參數(shù)包括:

belongsTo('關(guān)聯(lián)模型名','外鍵名','關(guān)聯(lián)表主鍵名',['模型別名定義'],'join類型');

默認(rèn)的關(guān)聯(lián)外鍵是user_id,如果不是,需要在第二個參數(shù)定義

<?php
namespace appindexmodel;

use thinkModel;

class Profile extends Model 
{
    public function user()
    {
        return $this->belongsTo('User','uid');
    }
}

我們就可以根據(jù)檔案資料來獲取用戶模型的信息

$profile = Profile::get(1);
// 輸出User關(guān)聯(lián)模型的屬性
echo $profile->user->account;

綁定屬性到父模型(V5.0.4+

可以在定義關(guān)聯(lián)的時候使用bind方法綁定屬性到父模型,例如:

<?php
namespace appindexmodel;

use thinkModel;

class User extends Model 
{
    public function profile()
    {
        return $this->hasOne('Profile','uid')->bind('nickname,email');
    }
}

或者使用數(shù)組的方式指定綁定屬性別名

<?php
namespace appindexmodel;

use thinkModel;

class User extends Model 
{
    public function profile()
    {
        return $this->hasOne('Profile','uid')->bind([
        		'email',
                'truename'	=> 'nickname',
                'profile_id'  => 'id',
            ]);
    }
}

然后使用關(guān)聯(lián)預(yù)載入查詢的時候,可以使用

$user = User::get(1,'profile');
// 輸出Profile關(guān)聯(lián)模型的email屬性
echo $user->email;
echo $user->profile_id;

綁定關(guān)聯(lián)屬性不影響原有關(guān)聯(lián)屬性的讀取,綁定關(guān)聯(lián)模型的屬性支持讀取器。

如果不是預(yù)載入查詢,請使用模型的appendRelationAttr方法追加屬性。

關(guān)聯(lián)自動寫入(V5.0.5+

我們可以使用together方法更方便的進行關(guān)聯(lián)自動寫入操作。

寫入

$blog = new Blog;
$blog->name = 'thinkphp';
$blog->title = 'ThinkPHP5關(guān)聯(lián)實例';
$content = new Content;
$content->data = '實例內(nèi)容';
$blog->content = $content;
$blog->together('content')->save();

更新

// 查詢
$blog = Blog::get(1);
$blog->title = '更改標(biāo)題';
$blog->content->data = '更新內(nèi)容';
// 更新當(dāng)前模型及關(guān)聯(lián)模型
$blog->together('content')->save();

刪除

// 查詢
$blog = Blog::get(1);
// 刪除當(dāng)前及關(guān)聯(lián)模型
$blog->together('content')->delete();
文檔最后更新時間:2018-06-09 15:33:38

文檔
目錄

深色
模式

切換
寬度