CakePHP在两个find数组上循环
所以我只是想在我查询的两个数组上循环CakePHP在两个find数组上循环,php,cakephp,cakephp-2.3,Php,Cakephp,Cakephp 2.3,所以我只是想在我查询的两个数组上循环 $draftedPlayers = $this->DraftedPlayer->find ( 'all', array ( 'conditions' => array ( 'leagueId' => $draft ['Draft'] ['id'] ) )); 及 我只想从$draf
$draftedPlayers = $this->DraftedPlayer->find ( 'all', array (
'conditions' => array (
'leagueId' => $draft ['Draft'] ['id']
)
));
及
我只想从
$draftedPlayers
中删除所有$draftedPlayers['draftedPlayers']['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次(1000000条if语句)
一个更好的比较方法是将被征召球员加入球员,并设置一个条件,使其无法找到匹配项,例如:
$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
)
));
尚未测试此代码,但这是总体思路。。。当然,最好是通过手动连接,而不是像上面那样进行手动连接 非常感谢你!我不知道有什么关联,我会试着走那条路。
$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
)
));