cakePHP返回重复记录 [英] cakePHP returning duplicate records
本文介绍了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 ')
)
),
'contains'=> 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: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:由于天气不好,我们今天的兑换会议将被取消。,
timestamp:2011-02-01 14:16:21,
playgroup_id: 2,
timeSince:1296591381000
},
{
id:1,
text: 2月1日星期二下午6:30在格林县图书馆站。
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:由于天气不好,我们今天的兑换会议将被取消。 ,
timestamp:2011-02-01 14:16:21,
playgroup_id:2,
timeSince:1296591381000
},
{
id:1,
text:记住我们在2月1日星期二开会在格林县图书馆站下午6:30。
timestamp:2011-01-28 23:01:57,
playgroup_id:2,
timeSince:1296277317000
} b $ b]
}
],
status:200
}
$ b使用连接 - GROUP BY sql语句对我来说最适合删除多个相同数据的出现:
$ b
$ b
$ this-> find('all',array('group'=> 'Playgroup.id','join'=>等等....));
For some reason cakePHP is giving me 2 copies of one result. Can anyone tell me why and how to fix it? Right now it's not a big deal, but once I get multiple playgroups for a single player, then the file download size increases.
Here's the model code:
$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')
));
And the result:
{
"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
}
解决方案
With using joins - the GROUP BY sql statement worked best for me to remove multiple occurences of the same data:
$this->find('all', array('group' => 'Playgroup.id', 'join' => etc....));
这篇关于cakePHP返回重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文