2014-02-22 34 views
0

我有我的狀態帖子的以下數據庫設置。對於每篇文章,用戶可以喜歡帖子,對帖子發表評論,甚至可以由作者在原帖中添加標籤。Laravel社交網絡的雄辯ORM

我想設置我的Resourceful控制器'發佈'通過JSON對象帶回所有數據,但我無法正確地找到評論,喜歡或標記用戶名。如果這有所作爲,我使用Sentry 2進行身份驗證。

這裏的數據庫設置:

CREATE TABLE Users (
     id INT NOT NULL AUTO_INCREMENT, 
     first_name VARCHAR(30), 
     last_name VARCHAR(30), 
     many more... 
    ); 

    CREATE TABLE Posts (
     postID INT NOT NULL AUTO_INCREMENT, 
     caption VARCHAR(200), 
     description VARCHAR(200), 
     fromID INT(10) UNSIGNED NOT NULL, 
     toID INT(10) UNSIGNED NOT NULL, 
     icon VARCHAR(200), 
     link VARCHAR(200), 
     message TEXT, 
     storyType INT, 
     type ENUM ('LINK', 'PHOTO', 'STATUSUPDATE', 'VIDEO'), 
     createdTime DATE, 
     PRIMARY KEY (postID), 
     FOREIGN KEY (fromID) REFERENCES users (id), 
     FOREIGN KEY (toID) REFERENCES users (id) 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

    CREATE TABLE Likes (
     likeID INT NOT NULL AUTO_INCREMENT, 
     fromID INT(10) UNSIGNED NOT NULL, 
     postID INT NOT NULL, 
     createdDate DATE, 
     PRIMARY KEY (likeID), 
     FOREIGN KEY (fromID) REFERENCES users (id), 
     FOREIGN KEY (postID) REFERENCES Posts (postID) 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

    CREATE TABLE Comments (
     commentID INT NOT NULL AUTO_INCREMENT, 
     fromID INT(10) UNSIGNED NOT NULL, 
     postID INT NOT NULL, 
     comment TEXT, 
     createdDate DATE, 
     PRIMARY KEY (commentID), 
     FOREIGN KEY (fromID) REFERENCES users (id), 
     FOREIGN KEY (postID) REFERENCES Posts (postID) 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 


    CREATE TABLE Tags (
     tagID INT NOT NULL AUTO_INCREMENT, 
     userID INT(10) UNSIGNED NOT NULL, 
     postID INT NOT NULL, 
     PRIMARY KEY (tagID), 
     FOREIGN KEY (userID) REFERENCES users (id), 
     FOREIGN KEY (postID) REFERENCES Posts (postID) 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

我的帖子控制器,我只是有一個簡單的頁面,吐出來的一切。我不想在我的視圖文件中遍歷任何內容,我只想帶回json完整對象。

class PostController extends BaseController { 

    public function show($id) 
    { 
    $post = Post::with(array('comments', 'from', 'tags', 'likes'))->find($id); 
    return View::make('samplepage')->with('data', $post); 
    } 
} 

我的帖子型號:

class Post extends Eloquent { 

    protected $table = 'Posts'; 
    protected $primaryKey = 'postID'; 

    public function comments() 
    { 
     return $this->hasMany('Comment','postID'); 
    } 

    public function tags() 
    { 
     return $this->hasMany('Tag','postID'); 
    } 

    public function likes() 
    { 
     return $this->hasMany('Like','postID'); 
    } 


    public function from() 
    { 
     return $this->belongsTo('User', 'fromID')->select(array('id', 'first_name', 'last_name')); 
    } 

    public function users() 
    { 
     return $this->belongsTo('User', 'fromID'); 
    } 

} 

評論模型: 類註釋擴展雄辯{

protected $table = 'Comments'; 
    protected $primaryKey = 'commentID'; 

    public function post() { 
     return $this->belongsTo('Post','fromID'); 
    } 

    public function user() { 
     return $this->belongsTo('User', 'fromID')->select(array('id', 'first_name', 'last_name')); 
    } 
} 

標籤型號: 類標籤擴展雄辯{

protected $table = 'Tags'; 
    protected $primaryKey = 'tagID'; 

} 

我甚至在我的用戶模型中設置了以下內容,但沒有任何區別。 用戶模型:

public function posts() { 
    return $this->hasMany('Post','id'); 
} 

public function comments() { 
return $this->hasMany('Comment','id'); 
} 

一切的偉大工程,在此設置下,當我打的職位/ 2與此下面的代碼,我得到下面的物體後面。 $ post = Post :: with(array('comments','from','tags','likes')) - > find($ id); 返回查看:: make('samplepage') - > with('data',$ post);

{ 
    postID: "2", 
    toID: "8", 
    comments: [ 
     { 
     commentID: "2", 
     comment: "second comment", 
     fromID: "1", 
     postID: "2", 
     createdDate: "2014-02-15" 
     } 
    ], 
    from: { 
     id: "4", 
     first_name: Paul, 
     last_name: Davis 
    }, 
    tags: [ 
     { 
     tagID: "1", 
     userID: "2", 
     postID: "2" 
     }, 
     { 
     tagID: "2", 
     userID: "3", 
     postID: "2" 
     } 
    ], 
    likes: [ 
     { 
     likeID: "1", 
     fromID: "2", 
     postID: "2", 
     createdDate: "2013-01-04" 
     }, 
     { 
     likeID: "2", 
     fromID: "3", 
     postID: "2", 
     createdDate: "2013-02-05" 
     } 
    ] 
} 

但我要的是以下內容,其中每個標籤,喜歡和評論以連接第一和最後一個名字,並讓他們回來的對象。

{ 
    postID: "2", 
    toID: "4", 
    comments: [ 
     { 
     commentID: "2", 
     comment: "second comment", 
     fromID: "1", 
     from: { 
       "name": "Jason Terry", 
       "id": "721286625" 
      }, 
     postID: "2", 
     createdDate: "2014-02-15" 
     } 
    ], 
    from: { 
     id: "4", 
     first_name: Paul, 
     last_name: Davis 
    }, 
    tags: [ 
     { 
     tagID: "1", 
     userID: "2", 
     from: { 
       "name": "David Lee", 
       "id": "721286625" 
      }, 
     postID: "2" 
     }, 
     { 
     tagID: "2", 
     userID: "3", 
     from: { 
       "name": "Paul Pierce", 
       "id": "721286625" 
      }, 
     postID: "2" 
     } 
    ], 
    likes: [ 
     { 
     likeID: "1", 
     fromID: "2", 
     from: { 
       "name": "David Lee", 
       "id": "721286625" 
      }, 
     postID: "2", 
     createdDate: "2013-01-04" 
     }, 
     { 
     likeID: "2", 
     fromID: "3", 
     from: { 
       "name": "Al Davis", 
       "id": "721286625" 
      }, 
     postID: "2", 
     createdDate: "2013-02-05" 
     } 
    ] 
} 

找遍了#1,無數Laravel博客,2周,現在的官方文檔,我似乎無法解決這個問題。任何幫助都非常感謝。

更新: 隨着託尼的回答下面我加

$post = Post::with(array('comments.users', 'from', 'tags.users', 'likes.users'))->find($id); 

然後我說

public function users() 
{ return $this->belongsTo('User', 'fromID')->select(array('id', 'first_name', 'last_name')); 
} 

的評論,標籤和喜歡的模式。現在這個對象很好用。

但我的調試器顯示以下

select `id`, `first_name`, `last_name` from `users` where `users`.`id` in ('1') 

select `id`, `first_name`, `last_name` from `users` where `users`.`id` in ('4') 

select `id`, `first_name`, `last_name` from `users` where `users`.`id` in ('2', '3') 

select `id`, `first_name`, `last_name` from `users` where `users`.`id` in ('2', '3') 

總之,它運行4個查詢我的用戶表。這不是多餘的嗎?它不應該對用戶表執行1個查詢,而不是對原始發佈用戶進行1次查詢,對查詢用戶進行1次查詢,對用戶進行1次查詢,對類似用戶進行1次查詢?

回答

3

它看起來像你想要使用嵌套關係。

$post = Post::with('comments.from', 'from', 'tags.from', 'likes.from')->find($id); 

您還需要將「從」關係編碼到每個模型中。

爲了獲得連接的名稱;你需要在你的用戶模型

protected $appends = array('name'); 
protected $hidden = array('first_name', 'last_name'); //this is optional 

public function getNameAttribute() 
{ 
    return $this->attributes['first_name'] . ' ' . $this->attributes['last_name']; 
} 
+0

託尼嗨下面,我添加 $ =後::後用( array('comments.users','from','tags','likes')) - > find($ id);只用評論進行測試。 然後,我添加 公共功能的用戶(){ 返回$這 - >屬於關聯( '用戶', 'fromID') - >選擇(陣列( 'ID', 'first_name的', '姓氏')); } 到評論模型。對象很好。但我的調試器顯示以下 從'users'選擇'id','first_name','last_name'其中( '1')'users'.'id' 選擇'id','first_name' ,'用戶'的'last_name'其中用戶''''''('4') 對用戶表進行2 db調用不是多餘的嗎? – user1011713

+0

我不應該通過評論模型來得到以下2條評論:從'users'選擇'id','first_name','last_name',其中用戶'.''在('1','3 '),所以這很好。 – user1011713

+0

@ user1011713如果您不介意,請使用此信息編輯原始帖子,以便閱讀。我不確定你在問什麼。 – TonyArra

0

是有點老的文章,但你也可以做到這一點,

public function from() 
    { 
     return $this->belongsTo('User', 'fromID') 
        ->select(array('id',DB::raw("CONCAT(firstname,' ', lastname) as name"))); 
    }