cakePHP返回重复记录
出于某种原因,cakePHP给了我一个结果的两个副本。谁能告诉我为什么以及如何修复它?现在这没什么大不了的,但是一旦我为一个玩家获得了多个游戏组,那么文件下载量就会增加 以下是模型代码:cakePHP返回重复记录,php,cakephp,Php,Cakephp,出于某种原因,cakePHP给了我一个结果的两个副本。谁能告诉我为什么以及如何修复它?现在这没什么大不了的,但是一旦我为一个玩家获得了多个游戏组,那么文件下载量就会增加 以下是模型代码: $this->recursive = -1; return $this->find('all', array( 'order' => array( 'PlaygroupActivity.timestamp
$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
}
为find()引入'fields'键,列出返回结果中需要的所有字段,包括'DISTINCT Playgroup.id'和使用连接-GROUP BY sql语句最适合我删除相同数据的多次出现:
$this->find('all', array('group' => 'Playgroup.id', 'join' => etc....));
对于我来说,字段添加不起作用,但对于分页添加:
$this->paginate = array_merge_recursive($this->paginate, array('group' => '`Vehicle`.`id`'));//for duplicate removal
奥列格斯方法也会起作用;我正在使用两个的组合来连接。不要!真不敢相信我居然没想到groupBY!请注意,尽管这在某些MySQL设置中可能有效(我所说的“工作”是指不会导致错误),但这不是一个正确的查询,结果是未定义的。在GROUPBY子句中,需要指定所有非聚合字段。这也意味着你可能不会,在某些时候也不会,得到你想要的结果。现在,如果你想每个玩家只有一个游戏组,你需要指定哪一个!