多態關聯
| 版本 | 功能調整 |
|---|---|
| 5.0.8 | 支持多態一對一關聯 |
| 5.0.4 | 支持多態一對多關聯 |
多態一對多關聯(V5.0.4+)
多(duo)態(tai)關(guan)聯允許一個模(mo)型(xing)在(zai)單個關(guan)聯定義方法中(zhong)從(cong)屬一個以上其(qi)它模(mo)型(xing),例如用戶可以評(ping)論書和文章,但評(ping)論表通常都是同一個數據表的設計。多(duo)態(tai)一對多(duo)關(guan)聯關(guan)系(xi),就是為了滿足類似的使(shi)用場景而設計。
下(xia)面是關聯(lian)表的數據表結構(gou):
article
id - integer
title - string
content - text
book
id - integer
title - string
comment
id - integer
content - text
commentable_id - integer
commentable_type - string
有兩個需要注意的字段是 comment 表中的 commentable_id 和 commentable_type我們稱之為多態字段。其中, commentable_id 用于存放書或者文章的 id(主鍵) ,而 commentable_type 用于存放所屬模型的類型。通常的設計是多態字段有一個公共的前綴(例如這里用的commentable),當然,也支持設置完全不同的字段名(例如使用data_id和type)。
多態關聯定義
接著(zhu),讓我們來(lai)查看創建這種關(guan)聯(lian)所需的模型定(ding)義:
文章模型:
<?php
namespace app\index\model;
use think\Model;
class Article extends Model
{
/**
* 獲取所有針對(dui)文章的評(ping)論。
*/
public function comments()
{
return $this->morphMany('Comment', 'commentable');
}
}
morphMany方法的參數如下:
morphMany('關聯模型名','多態字段信息','多態類型');
關(guan)聯模型名(必須):關聯的模型名稱,可以使用模型名(如Comment)或者完整的命名空間模型名(如app\index\model\Comment)。
多態字段(duan)信息(可選):支持兩種方式定義 如果是字符串表示多態字段的前綴,多態字段使用 多態前綴_type和多態前綴_id,如果是數(shu)組,表(biao)示使用(yong)['多態類(lei)型(xing)字(zi)段名(ming)(ming)','多態ID字(zi)段名(ming)(ming)'],默認為(wei)(wei)當前的(de)關聯方法(fa)名(ming)(ming)作為(wei)(wei)字(zi)段前綴(zhui)。
多態類型(xing)(可選):當前模型對應的多態類型,默認為當前模型名,可以使用模型名(如Article)或者完整的命名空間模型名(如app\index\model\Article)。
書籍模型:
<?php
namespace app\index\model;
use think\Model;
class Book extends Model
{
/**
* 獲取所有針對書籍的評論。
*/
public function comments()
{
return $this->morphMany('Comment', 'commentable');
}
}
書籍模型(xing)的設(she)置(zhi)方法(fa)同(tong)(tong)文(wen)章模型(xing)一致,區別在于多(duo)態類(lei)型(xing)不同(tong)(tong),但由于多(duo)態類(lei)型(xing)默認會取當前模型(xing)名(ming),因此(ci)不需要單獨(du)設(she)置(zhi)。
下面(mian)是評論(lun)模型的關聯定義:
<?php
namespace app\index\model;
use think\Model;
class Comment extends Model
{
/**
* 獲(huo)取評論對應的(de)多態模型。
*/
public function commentable()
{
return $this->morphTo();
}
}
morphTo方法的參數如下:
morphTo('多態字段信息',['多態類型別名']);
多態字段信息(可選):支持兩種方式定義 如果是字符串表示多態字段的前綴,多態字段使用 多態前綴_type和多態前綴_id,如果是數組,表示使用['多態類型字段名','多態ID字段名'],默認為當前的關聯方法名作為字段前綴
多態(tai)類型別名(ming)(可選):數組方式定義
獲取多態關聯
一旦你的數據表及模型被定義,則可以通過模型來訪問關聯。例如,若要訪問某篇文章的所有評論,則可以簡單的使用 comments 動態屬(shu)性:
$article = Article::get(1);
foreach ($article->comments as $comment) {
dump($comment);
}
你也可以從多態模型的多態關聯中,通過訪問調用 morphTo 的方法名稱來獲取擁有者,也就是此例子中 Comment 模型的 commentable 方法。所以,我(wo)們(men)可以使用動態(tai)屬性(xing)來訪問這個方法:
$comment = Comment::get(1);
$commentable = $comment->commentable;
Comment 模型的 commentable 關聯會返回 Article 或 Book 模型(xing)的對象實例,這取(qu)決于評論所屬模型(xing)的類型(xing)。
自定義多態關聯的類型字段
默認情況下,ThinkPHP 會使用模型名作為多態表的類型區分,例如,Comment屬于 Article 或者 Book , commentable_type 的默認值可以分別是 Article 或者 Book 。我們可以(yi)通過定義多態(tai)的(de)時(shi)候傳入參(can)數來對數據庫(ku)進行解耦。
public function commentable()
{
return $this->morphTo('commentable',[
'book' => 'app\index\model\Book',
'post' => 'app\admin\model\Article',
]);
}
多態一對一關聯(V5.0.8+)
多(duo)態(tai)一(yi)(yi)對(dui)一(yi)(yi)相比多(duo)態(tai)一(yi)(yi)對(dui)多(duo)關(guan)(guan)聯的(de)區別是動(dong)態(tai)的(de)一(yi)(yi)對(dui)一(yi)(yi)關(guan)(guan)聯,舉個例子(zi)說有一(yi)(yi)個個人和團隊表(biao),而無(wu)論個人還(huan)是團隊都有一(yi)(yi)個頭像需要保存但都會對(dui)應同(tong)一(yi)(yi)個頭像表(biao)
member
id - integer
name - string
team
id - integer
name - string
avatar
id - integer
avatar - string
imageable_id - integer
imageable_type - string
會員模型:
<?php
namespace app\index\model;
use think\Model;
class Member extends Model
{
/**
* 獲取(qu)用(yong)戶的(de)頭像
*/
public function avatar()
{
return $this->morphOne('Avatar', 'imageable');
}
}
團隊模型:
<?php
namespace app\index\model;
use think\Model;
class Team extends Model
{
/**
* 獲(huo)取團隊的頭像
*/
public function avatar()
{
return $this->morphOne('Avatar', 'imageable');
}
}
morphOne方法的參數如下:
morphOne('關聯模型名','多態字段信息','多態類型');
關聯模型(xing)名(必須):關聯的模型名稱,可以使用模型名(如Member)或者完整的命名空間模型名(如app\index\model\Member)。
多態字段(duan)信息(可選):支持兩種方式定義 如果是字符串表示多態字段的前綴,多態字段使用 多態前綴_type和多態前綴_id,如果是(shi)數組,表(biao)示(shi)使用['多態(tai)類(lei)型字(zi)段名(ming)','多態(tai)ID字(zi)段名(ming)'],默(mo)認(ren)為當前的(de)關聯方法名(ming)作為字(zi)段前綴。
多(duo)態類型(可選):當前模型對應的多態類型,默認為當前模型名,可以使用模型名(如Member)或者完整的命名空間模型名(如app\index\model\Member)。
下面是頭像模型的(de)關聯定義:
<?php
namespace app\index\model;
use think\Model;
class Avatar extends Model
{
/**
* 獲取頭像(xiang)對應的多態模型。
*/
public function imageable()
{
return $this->morphTo();
}
}
