Ich habe derzeit 3 ??Tische,tags
、product
、album
.
product
、album
相關(guān)聯(lián)的標簽都在tags
里面。通過biz_type
進行區(qū)分,1表示product
, 2表示album
.
Die Tabellenbeziehung ist wie folgt
table_tags
id: int
biz_type: int
biz_id: int
table_product
id: int
table_album
id: int
Ich hoffe jetzt, die polymorphe Assoziation von tags
的分頁列表,去關(guān)聯(lián)獲取product
和album
的信息。
目前查看了laravel
abzufragen, aber es scheint, dass dieser Ansatz nicht unterstützt wird. Gibt es eine M?glichkeit, durch Bedingungen eine Beziehung herzustellen?
Laravel的多態(tài)的多對多關(guān)聯(lián)有講到這個,這個使用場景跟你有點類似你可以看看
http://www.kancloud.cn/baidu/...
Polymorphic Many To Many Relation Table Structure 多態(tài)的多對多關(guān)聯(lián)數(shù)據(jù)庫表結(jié)構(gòu)
除了一般的多態(tài)關(guān)聯(lián),也可以使用多對多的多態(tài)關(guān)聯(lián)。例如,Blog 的 Post 和 Video 模型可以共用多態(tài)的 Tag 關(guān)聯(lián)模型。首先,來看看數(shù)據(jù)庫表結(jié)構(gòu):
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
現(xiàn)在,我們準備好設(shè)定模型關(guān)聯(lián)了。 Post 和 Video 模型都可以經(jīng)由 tags 方法建立 morphToMany 關(guān)聯(lián):
class Post extends Model {
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
}
在 Tag 模型里針對每一種關(guān)聯(lián)建立一個方法:
class Tag extends Model {
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
public function videos()
{
return $this->morphedByMany('App\Video', 'taggable');
}
}
自己實現(xiàn)一個關(guān)聯(lián)查詢也可以啊,目前多態(tài)關(guān)聯(lián)的條件查詢因為過于復(fù)雜,框架并未提供。