2011-11-02 116 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