一對一關聯
| 版本 | 功能調整 |
|---|---|
| 5.0.5 | 增加關聯自動寫入和刪除 |
| 5.0.4 | 增加關聯屬性綁定到父模型功能 |
定義
定義一對一關聯,例如,一個用戶都有一個個人資料,我們定義User模型如下:
namespace appindexmodel;
use thinkModel;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile');
}
}
hasOne方法的參數包括:
hasOne('關聯模型名','外鍵名','主鍵名',['模型別名定義'],'join類型');
默認的join類型為INNER。
V5.0.3+版本開(kai)始,可以支持為關聯模(mo)型定義需(xu)要查詢的字段(duan),例如:
namespace appindexmodel;
use thinkModel;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile')->field('id,name,email');
}
}
如果使用的是
join方式的(de)關聯,不支持指定field字段(duan)。
5.0.5+版本開始,模(mo)型別名定義參數(shu)已經廢棄。
關聯查找
定義好關聯之后,就可以(yi)使用下面的(de)方法獲取關聯數據:
$user = User::get(1);
// 輸出Profile關聯模型的email屬性(xing)
echo $user->profile->email;
如果要根據關聯表的查詢條件查詢當前模型的數據,可以使用hasWhere方法,例如:
$user = User::hasWhere('profile',['email'=>'thinkphp@qq.com'])->find();
echo $user->name;
默認情況下, 我們使用的是user_id 作(zuo)為外鍵關聯,如果不(bu)是(shi)的(de)(de)話則(ze)需(xu)要在(zai)關聯定義的(de)(de)時候指定,例如:
<?php
namespace appindexmodel;
use thinkModel;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile','uid');
}
}
有一點需要注意的是,關聯方法的命名規范是駝峰法,而關聯屬性則一般是小寫+下劃線的方式,系統在獲取的時候會自動轉換對應,讀取
user_profile關聯屬性則對應的關聯方法應該是userProfile。
關聯新增
$user = User::get(1);
// 如果(guo)還沒有關聯數(shu)據(ju) 則進行新增
$user->profile()->save(['email' => 'thinkphp']);
系(xi)統會自動把(ba)當(dang)前模(mo)型(xing)的(de)主鍵傳(chuan)入profile模(mo)型(xing)。
關聯更新
和新增一樣使用save方法進行更新關聯(lian)數據(ju)。
$user = User::get(1);
$user->profile->email = 'thinkphp';
$user->profile->save();
// 或者
$user->profile->save(['email' => 'thinkphp']);
定義相對的關聯
我們可以在Profile模(mo)型中定義一個相對的(de)關(guan)聯關(guan)系,例如:
namespace appindexmodel;
use thinkModel;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
belongsTo的參數包括:
belongsTo('關聯模型名','外鍵名','關聯表主鍵名',['模型別名定義'],'join類型');
默認的關聯外鍵是user_id,如果不是,需(xu)要在第(di)二個參(can)數定義
<?php
namespace appindexmodel;
use thinkModel;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('User','uid');
}
}
我(wo)們就可以根(gen)據(ju)檔案資料來獲取用戶模型的信息
$profile = Profile::get(1);
// 輸出User關聯模型的(de)屬性
echo $profile->user->account;
綁定屬性到父模型(V5.0.4+)
可以(yi)在定(ding)義(yi)關聯(lian)的時候使(shi)用bind方法綁定(ding)屬性到父模型,例如(ru):
<?php
namespace appindexmodel;
use thinkModel;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile','uid')->bind('nickname,email');
}
}
或者使用(yong)數(shu)組的方式指定綁定屬(shu)性別名
<?php
namespace appindexmodel;
use thinkModel;
class User extends Model
{
public function profile()
{
return $this->hasOne('Profile','uid')->bind([
'email',
'truename' => 'nickname',
'profile_id' => 'id',
]);
}
}
然后使(shi)用關聯(lian)預載入(ru)查詢的時候(hou),可(ke)以(yi)使(shi)用
$user = User::get(1,'profile');
// 輸(shu)出Profile關(guan)聯模(mo)型的email屬性
echo $user->email;
echo $user->profile_id;
綁(bang)定關聯屬性(xing)不影響(xiang)原有關聯屬性(xing)的讀(du)取,綁(bang)定關聯模型的屬性(xing)支(zhi)持讀(du)取器。
如果不是預載入(ru)查詢,請(qing)使(shi)用(yong)模(mo)型的appendRelationAttr方法追(zhui)加屬(shu)性(xing)。
關聯自動寫入(V5.0.5+)
我們可以使用together方(fang)法更方(fang)便(bian)的進行關(guan)聯自動寫入操作(zuo)。
寫入
$blog = new Blog;
$blog->name = 'thinkphp';
$blog->title = 'ThinkPHP5關聯(lian)實(shi)例';
$content = new Content;
$content->data = '實(shi)例(li)內容(rong)';
$blog->content = $content;
$blog->together('content')->save();
更新
// 查詢
$blog = Blog::get(1);
$blog->title = '更改(gai)標題';
$blog->content->data = '更新(xin)內容';
// 更新(xin)當前模型及關聯模型
$blog->together('content')->save();
刪除
// 查(cha)詢(xun)
$blog = Blog::get(1);
// 刪除當(dang)前(qian)及關聯模型
$blog->together('content')->delete();
