我有以下代码:
$dateCounter=1;
foreach($teamsA as $teamHome){
foreach($teamsACopy as $teamAway){
if($teamHome!=$teamAway){
$tactic1 = $doctrine->getRepository('LoginLoginBundle:Tactics')
->findByteamTeamid($teamHome->getTeamid());
$tactic2 = $doctrine->getRepository('LoginLoginBundle:Tactics')
->findByteamTeamid($teamAway->getTeamid());
$match = new Match();
$match->setAwayteam($teamAway->getName());
$match->setHometeam($teamHome->getName());
$match->setIsplayed(false);
$match->setSeasonSeasonid($season);
$date = new DateTime(date('Y-m-d H:i:s'));
$date->add(new DateInterval('P'.$dateCounter.'D'));
$match->setDate($date);
$dateCounter++;
$tacticMatchHome = new Tactics();
$tacticMatchHome = $tactic1[0];
$tacticMatchHome->setMatchMatchid($match);
$tacticMatchAway = new Tactics();
$tacticMatchAway = $tactic2[0];
$tacticMatchAway->setMatchMatchid($match);
$em->persist($match);
$em->flush();
$em->persist($tacticMatchHome);
$em->flush();
$em->persist($tacticMatchAway);
$em->flush();
}
}
}
当它尝试将其保存到数据库时,我收到以下错误:
[Doctrine'DBAL'DBALException]
An exception occurred while executing 'INSERT INTO match (date, homeTeam, awayTeam, winner, isPlayed, score, season_seasonid) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["2014-11-16 13:50:24", "FC Beer", "Soccerteam11", null, 0, null, 9]:
SQLSTATE[42000]: Syntax error or acces violation: 1064 You have an error in your SQL syntax: check the manual that corresponds to your MySQL server version for the right syntax to use near 'match (date, homeTeam, awayTeam, winner, isPlayed, score, season_seasonid) VALUE' at line1
是否有可能因为我的日期格式不正确而收到此错误?如果是这样,如何将其转换为正确的格式?
我正在使用 Symfony2 和 PHP 5.4 版
match
是一个MySQL保留关键字。如果要将表命名为"匹配",则必须将其包装在刻度中:
'INSERT INTO `match` (date, homeTeam, awayTeam, winner, isPlayed, score, season_seasonid) VALUES (?, ?, ?, ?, ?, ?, ?)'
将表名匹配更改为其他 - 这是一个保留字。不要仅为代码完成而创建新对象。将$tacticMatchHome = new Tactics();
替换为 /** @var $tacticMatchHome Tactics */
。并使用 findOneBy 而不是 findBy ;)最后,您可以在代码末尾刷新一次。