2011-11-02 111 views
1

由於某種原因,cakePHP給了我一份結果的兩份副本。誰能告訴我爲什麼以及如何解決它?現在它不是什麼大不了的事情,但是一旦我爲單個玩家獲得多個遊戲組,那麼文件下載大小就會增加。cakePHP返回重複記錄

這裏的型號代碼:

 $this->recursive = -1; 
     return $this->find('all', array(
      'order' => array(
       'PlaygroupActivity.timestamp DESC' 
      ), 
      'limit' => $limit, 
      'joins' => array(
       array(
        'table' => 'playgroup_players', 
        'alias' => 'PlaygroupPlayers', 
        'type' => 'inner', 
        'foreignKey' => false, 
        'conditions'=> array('PlaygroupPlayers.playgroup_id = Playgroup.id', 'PlaygroupPlayers.player_id'=>$id) 
       ), 
       array(
        'table' => 'playgroup_activities', 
        'alias' => 'PlaygroupActivity', 
        'type' => 'inner', 
        'foreignKey' => false, 
        'conditions'=> array('PlaygroupActivity.playgroup_id = Playgroup.id') 
       ) 
      ), 
      'contain'=> array('PlaygroupActivity') 
     )); 

而結果:

{ 
    "data":[ 
     { 
     "Playgroup":{ 
      "id":"3", 
      "name":"South Florida Redemption", 
      "email":null, 
      "email_username":null, 
      "email_password":null, 
      "tag":null, 
      "description":null, 
      "avatar_url":null, 
      "city":null, 
      "state":null 
     }, 
     "PlaygroupActivity":[ 
      { 
       "id":"3", 
       "text":"FINAL RESULTS OF THE FLORIDA STATE TOURNAMENT ", 
       "timestamp":"2011-11-02 00:13:33", 
       "playgroup_id":"3", 
       "timeSince":"1320210813000" 
      } 
     ] 
     }, 
     { 
     "Playgroup":{ 
      "id":"2", 
      "name":"Redemption Springfield", 
      "email":null, 
      "email_username":null, 
      "email_password":null, 
      "tag":null, 
      "description":null, 
      "avatar_url":null, 
      "city":null, 
      "state":null 
     }, 
     "PlaygroupActivity":[ 
      { 
       "id":"2", 
       "text":"Due to bad weather, our Redemption meet today will be canceled.", 
       "timestamp":"2011-02-01 14:16:21", 
       "playgroup_id":"2", 
       "timeSince":"1296591381000" 
      }, 
      { 
       "id":"1", 
       "text":"Remember we have a meet Tuesday, February 1rst at the Greene County Library Station at 6:30pm.", 
       "timestamp":"2011-01-28 23:01:57", 
       "playgroup_id":"2", 
       "timeSince":"1296277317000" 
      } 
     ] 
     }, 
     { 
     "Playgroup":{ 
      "id":"2", 
      "name":"Redemption Springfield", 
      "email":null, 
      "email_username":null, 
      "email_password":null, 
      "tag":null, 
      "description":null, 
      "avatar_url":null, 
      "city":null, 
      "state":null 
     }, 
     "PlaygroupActivity":[ 
      { 
       "id":"2", 
       "text":"Due to bad weather, our Redemption meet today will be canceled.", 
       "timestamp":"2011-02-01 14:16:21", 
       "playgroup_id":"2", 
       "timeSince":"1296591381000" 
      }, 
      { 
       "id":"1", 
       "text":"Remember we have a meet Tuesday, February 1rst at the Greene County Library Station at 6:30pm.", 
       "timestamp":"2011-01-28 23:01:57", 
       "playgroup_id":"2", 
       "timeSince":"1296277317000" 
      } 
     ] 
     } 
    ], 
    "status":200 
} 

回答

4

使用連接 - GROUP BY sql語句對我最有效的刪除了多個相同數據的出現:

$this->find('all', array('group' => 'Playgroup.id', 'join' => etc....)); 
+0

另外Olegs方法也能發揮作用;我正在使用兩者的組合來加入我的聯盟。 –

+0

do'h!不敢相信我沒想到groupBY! – LordZardeck

+0

請注意,雖然這可能在某些MySQL設置中起作用(並且我的意思是「不會導致錯誤),但這不是一個正確的查詢,結果是未定義的。在GROUP BY子句中,您需要指定所有非聚合字段。這也意味着你可能不會,並且在某些時候不會得到你想要的結果。現在,如果您只想爲每個播放器播放一個PlayGroup,則需要指定哪一個! –

0

推出「場」爲查找(鍵),列出所有將在返回的結果所需的字段,包括「 DISTINCT Playgroup.id'

0

對我來說,沒有工作的領域增加,但對分頁地址:

$this->paginate = array_merge_recursive($this->paginate, array('group' => '`Vehicle`.`id`'));//for duplicate removal