2016-07-18 79 views
0

我有3個表,一個news錶店的消息,songs錶店的歌曲,comments錶店的意見,新聞可以有很多評論,有首歌也可以有很多意見,我comments表,我使用item_id,item_type來確定此評論屬於哪個父模型。例如,如果item_type='game' and item_id=1比此評論屬於game表和遊戲ID = 1。 以下是我的型號代碼。雄辯屬於關聯確定關係表

歌曲

class Song 
{ 
    public function comment() 
    { 
     return $this->hasMany(Comment::class, 'item_id', 'id')->where('comments.item_type', '=', Comment::SONG_COMMENT_TYPE); 
    } 
} 

新聞

class News 
{ 
    public function comment() 
    { 
     return $this->hasMany(Comment::class, 'item_id', 'id')->where('comments.item_type', '=', Comment::NEWS_COMMENT_TYPE); 
    } 
} 

評論

class Comment 
{ 
    public function song() 
    { 
     return $this->belongsTo(Song::class, 'item_id', 'id'); 
    } 
    public function news() 
    { 
     return $this->belongsTo(News::class, 'item_id', 'id'); 
    } 
} 

可以很容易地從SongNews模型得到的意見,但我不知道如何定義反轉關係,是否可以在中定義方法210型號,如:

public function item() 
{ 
    if ($this->item_type == Comment::SONG_COMMENT_TYPE) { 
     return $this->song(); 
    } else { 
     return $this->news(); 
    } 
} 

或者,也許我在錯誤的方式在想,如何用雄辯來確定加入上運行時其表?

+0

它看起來像Laravel具有多晶的關係沒有反向(糾正我,如果我錯了)。我認爲這個解決方案對你想達到的目標很有幫助。 –

+0

@ThomasVanderVeen我還在數據庫設計階段爲我的應用程序,你有我的問題的任何建議? – xcaptain

+0

據我所知,您需要在'Comment'模型中爲每個關係定義一個方法。 – haakym

回答

1

要實現多態的關係,我認爲你的數據庫應該是這樣的:

news 
    id 
    ... 

songs 
    id 
    ... 

comments 
    id 
    commentable_id 
    commentable_type 

那麼你的每個模型的關係應該是這樣的:

class Comment extends Model 
{ 
    /** 
    * Get all of the owning commentable models. 
    */ 
    public function commentable() 
    { 
     return $this->morphTo(); 
    } 
} 

class News extends Model 
{ 
    /** 
    * Get all of the news' comments. 
    */ 
    public function comments() 
    { 
     return $this->morphMany('App\Comment', 'commentable'); 
    } 
} 

class Song extends Model 
{ 
    /** 
    * Get all of the song's comments. 
    */ 
    public function comments() 
    { 
     return $this->morphMany('App\Comment', 'commentable'); 
    } 
} 

閱讀資料這裏:https://laravel.com/docs/5.2/eloquent-relationships#polymorphic-relations

+0

感謝,它適用於我,我一開始並沒有想到要創建一個多態模型,我想這是一對多的關係。而且我還發現了一個很棒的帖子http://www.easylaravelbook.com/blog/2015/01/21/creating-polymorphic-relations-in-laravel-5/ – xcaptain

1

一個簡單的解決方案可以使用新聞模型作爲出發點,並將註釋表格與兩個簡單的表達式結合起來。

一個例子可能看起來像:

// Comment 

public function news() 
{ 

    return News:: 

     select('news.*') // Only select news columns (can be removed) 

     ->join('comments', 'news.id', '=', 'item_id') // Join comments table 

     ->where('item_type', 'news') // Make sure item type is news 

     ->where('comments.id', $this->id) // Get $this as comment 

     ->first(); // Return a single result. Maybe this is unnecessary, you 
        // should test it by removing this. I assume when this 
        // will be removed it will return an array of results, 
        // and that is not what you want. Right? 

} 

這個函數會返回一個結果或NULL。

通過改變一些名字,歌曲也可以做到這一點。

希望這有助於:)

+0

感謝您的辛勤工作,此解決方案也可以工作,但結果列表將會像「news」一樣:NULL,「song」:{},並不比那些多義的優雅。 – xcaptain