2012-08-11 91 views
1
所有選票

所以我連接我的兩個模型在一起,它的返回是這樣的:檢索來自兩個聯接的表

Array 
(
    [Submission] => Array 
     (
      [id] => 47 
      [user_id] => 0 
      [title] => asdfasdfsa dfasdf asdfa sfa fadf 
      [source] => http://www.aol.com 
      [slug] => 
      [category] => health 
      [created] => 2012-06-25 11:30:16 
     ) 

    [User] => Array 
     (
      [id] => 2 
      [username] => john 
     ) 

    [SubmissionsVote] => Array 
     (
      [0] => Array 
       (
        [id] => 247 
        [user_id] => 2 
        [submission_id] => 47 
        [vote_type] => up 
       ) 

     ) 

) 

然而,有時票的數量將是[0] - [N ]。這是我的$newestSubmissions。我做給我查詢是:

public function newestSubmissions() { 
    $this->unBindModel(
      array('hasMany' => array('Comment'))); 
    return $this->find('all', array(
       'fields' => array(
        'Submission.id', 
        'Submission.user_id', 
        'Submission.title', 
        'Submission.source', 
        'Submission.slug', 
        'Submission.category', 
        'Submission.created', 
        'User.id', 
        'User.username' 
       ), 
       'order' => 'Submission.created DESC' 
      )); 
} 

我想什麼做的是整個$newestSubmissions數組對象檢索所有的票以及所有的票(以下查詢)上的每一個在$newestSubmissions計算實際得分,所以我可以發送被叫$newestSubmissions我的看法單個陣列對象,而不是$newestSubmissions$submissionScore

public function getVoteType($userId, $submissionId) { 
    $voteType = $this->find('all', array(
     'conditions' => array(
      'User.id' => $userId, 
      'Submission.id' => $submissionId), 
     'fields' => array(
      'SubmissionsVote.vote_type'), 
     'limit' => '1' 
      )); 
    return $voteType[0]['SubmissionsVote']['vote_type']; 
} 

基本上我想要一個單一的分數發送到我的觀點與所有其他信息一起而不必發送內部索引數組obje ct在我的父數組對象(這是我目前正在獲取)。

我該怎麼做?

回答

0

好的......我試圖發表這個評論,但它太長了。這不是一個真正的答案。

你知道可容忍的行爲嗎?檢查出來 - 這是很好的確定什麼樣的數據,你回來:http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

另外,如果你要打破你的提交內容進行投票表分成兩個表 - upvotes和downvotes,那麼你也可以使用Cake的counterCache行爲保持跟蹤直接在'提交'表格中上/下投票的數量(您需要在提交表中添加upvote_count和downvote_count列來完成此操作)。

這將使計算提交評分更容易 - 並且您不必每次都從數據庫中提取所有投票。對於doco,請在此頁上搜索counterCache:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html這可能值得考慮。

+0

謝謝我會研究一下。這看起來更好的解決方案。 – 2012-08-11 19:12:57

0
return $this->find('all', array(
       'fields' => array(
        'Submission.id', 
        'Submission.user_id', 
        'Submission.title', 
        'Submission.source', 
        'Submission.slug', 
        'Submission.category', 
        'Submission.created', 
        'User.id', 
        'User.username', 
        'submissionsvotes.vote_type' 
       ), 
       'joins' => array(
          array(
          'table' => 'SubmissionsVote', 
          'alias' => 'submissionsvotes', 
          'type' => 'LEFT', 
          'conditions' => array 
          ('User.id = SubmissionsVotes.user_id','Submission.id'=>'SubmissionsVotes.submission_id') 
          ), 
         ) 


       'group'=>'submissionsvotes.vote_type', 
       'order' => 'Submission.created DESC' 
      ) 
    );