我有以下代码:
$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
答案 0 :(得分:1)
match
是MySQL保留的关键字。如果您要将表格命名为“匹配”,则必须将其打包为刻度:
'INSERT INTO `match` (date, homeTeam, awayTeam, winner, isPlayed, score, season_seasonid) VALUES (?, ?, ?, ?, ?, ?, ?)'
答案 1 :(得分:0)
将表名称匹配更改为其他 - 这是一个保留字。
不要仅为代码完成创建新对象。
将$tacticMatchHome = new Tactics();
替换为/** @var $tacticMatchHome Tactics */
。
并使用findOneBy而不是findBy;)
最后,您可以在代码结束时刷新一次。