我有以下代码:
$arraySlice1 = array_slice($teamsA, 0, 8);
$arraySlice2 = array_slice($teamsA, 8, 15);
foreach ($arraySlice1 as $teamHome) {
$dateCounter = 1;
foreach ($arraySlice2 as $teamAway) {
$tactic1 = $doctrine->getRepository('LoginLoginBundle:Tactics')
->findByteamTeamid($teamHome->getTeamid());
$tactic2 = $doctrine->getRepository('LoginLoginBundle:Tactics')
->findByteamTeamid($teamAway->getTeamid());
$match = new Matchgame();
$match->setAwayteam($teamAway->getName());
$match->setHometeam($teamHome->getName());
$match->setIsplayed(false);
$match->setSeasonSeasonid($season);
$date = new DateTime(date('Y-m-d'));
$date->add(new DateInterval('P' . $dateCounter . 'D'));
$match->setDate($date);
$dateCounter+=2;
$tacticMatchHome = $tactic1[0];
$tacticMatchHome->setMatchMatchid($match);
$tacticMatchAway = $tactic2[0];
$tacticMatchAway->setMatchMatchid($match);
$em->persist($match);
$em->flush();
$em->persist($tacticMatchHome);
$em->flush();
$em->persist($tacticMatchAway);
$em->flush();
}
}
foreach ($arraySlice2 as $teamHome) {
$dateCounter = 2;
foreach ($arraySlice1 as $teamAway) {
$tactic1 = $doctrine->getRepository('LoginLoginBundle:Tactics')
->findByteamTeamid($teamHome->getTeamid());
$tactic2 = $doctrine->getRepository('LoginLoginBundle:Tactics')
->findByteamTeamid($teamAway->getTeamid());
$match = new Matchgame();
$match->setAwayteam($teamAway->getName());
$match->setHometeam($teamHome->getName());
$match->setIsplayed(false);
$match->setSeasonSeasonid($season);
$date = new DateTime(date('Y-m-d'));
$date->add(new DateInterval('P' . $dateCounter . 'D'));
$match->setDate($date);
$dateCounter+=2;
$tacticMatchHome = $tactic1[0];
$tacticMatchHome->setMatchMatchid($match);
$tacticMatchAway = $tactic2[0];
$tacticMatchAway->setMatchMatchid($match);
$em->persist($match);
$em->flush();
$em->persist($tacticMatchHome);
$em->flush();
$em->persist($tacticMatchAway);
$em->flush();
}
}
这给了我以下数据库元素:
问题在于我不断获得Soccerteam18和FC Beer,这些应该是所有不同的球队。我的foreach循环有什么问题?
我像这样填写$ teamsA:
$teamsA = $doctrine->getRepository('LoginLoginBundle:Team')
->findBydivisionDivisionid($divisionA[0]->getDivisionid());