所以我只是尝试遍历我查询的两个数组。
$draftedPlayers = $this->DraftedPlayer->find ( 'all', array (
'conditions' => array (
'leagueId' => $draft ['Draft'] ['id']
)
));
和
$players = $this->Player->find('all');
我只是想从$players
中删除$draftedPlayers
的所有结果,其中$draftedPlayers['DraftedPlayer']['playerId']
等于$players['Player']['id']
cakePHP是否有一个内置的助手?我不太确定如何比较这两个物体。
一个简单的foreach
循环就可以达到你想要的效果:
foreach($players as &$player) { // loop the players (with referenced iterator)
foreach($draftedPlayers as $drafted_player) { // inner loop, drafted playerd
if($player['Player']['id'] == $drafted_player['DraftedPlayer']['playerId']) {
unset($player); // compare the keys and remove where matching
break; // skip the rest of the inner loop
}
}
}
然而,这取决于每个数组中有多少条目,这可能是1000个代表,内部循环为1000个代表(1,000,000个if语句)。
更好的比较方法是将 DraftedPlayer连接到Player,并设置一个条件,使其无法找到匹配,例如:
$players = $this->Player->find('all', array(
'joins' => array(
array(
'table' => 'drafted_players',
'alias' => 'DraftedPlayer',
'type' => 'LEFT',
'conditions' => array(
'DraftedPlayer.playerId = Player.id',
'DraftedPlayer.leagueId' => $draft['Draft']['id']
)
)
),
'conditions' => array(
// your general conditions here
'DraftedPlayer.playerId IS NULL' // only return un matched results from players table
// which essentially means the player is NOT drafted
)
));
还没有测试过这段代码,但这是一般的想法…当然,最好是通过CakePHP的模型关联来实现,而不是像上面那样手动连接。