cakePHP返回重复记录

时间:2011-11-02 07:02:36

标签: php cakephp

由于某种原因,cakePHP给了我一份结果的2份副本。任何人都可以告诉我为什么以及如何解决它?现在这不是什么大问题,但是一旦我为一个玩家获得多个游戏组,那么文件下载量会增加。

以下是型号代码:

        $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
}

3 个答案:

答案 0 :(得分:5)

使用联接 - GROUP BY sql语句最适合我删除多个相同数据的出现:

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

答案 1 :(得分:0)

为find()引入'fields'键,列出返回结果中所需的所有字段,包括'DISTINCT Playgroup.id'

答案 2 :(得分:0)

对我来说,这些字段没有添加,但是对于分页添加:

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