I have a PHP script using Doctrine 2 which does essentially the following:
我有一个使用Doctrine 2的PHP脚本,它主要执行以下操作:
$entityManager->transactional(function($em) {
$foreignObject = new DoctrineEntities\ForeignTable();
$em->persist($foreignObject);
$em->flush();
$aObject = new DoctrineEntities\A();
$aObject->ForeignID = $foreignObject->ID;
$em->persist($aObject);
$em->flush();
});
I'm getting an integrity constraint violation:
我收到了完整性约束违规:
a foreign key constraint fails (dbName.A, CONSTRAINT A_ForeignID FOREIGN KEY (ForeignID) REFERENCES
ForeignTable
(ID
) ON DELETE NO ACTION ON UPDATE NO ACTION)外键约束失败(dbName.A,CONSTRAINT A_ForeignID FOREIGN KEY(ForeignID)REFERENCES ForeignTable(ID)ON DELETE NO ACTION ON UPDATE NO ACTION)
My guess is that the constraint is checked before the commit, and it doesn't check to see whether an insert I made that hasn't been committed yet might make the constraint pass rather than fail. But I really do want these two insert statements wrapped in the same transaction. So what can I do?
我的猜测是在提交之前检查约束,并且它不会检查我所做的尚未提交的插入是否可能使约束通过而不是失败。但我真的希望这两个插入语句包含在同一个事务中。那我该怎么办?
UPDATE
I moved $em->persist($aObject); $em->flush();
out of the transaction and I'm still getting the same error. Apparently, my guess was wrong... But then I really don't know what's causing the error.
我移动了$ em-> persist($ aObject); $ EM->冲洗();退出交易,我仍然得到同样的错误。显然,我的猜测是错误的...但后来我真的不知道是什么导致错误。
SQL Context
Table A
CREATE TABLE `A` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`ForeignID` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `A_ForeignID` (`ForeignID`),
CONSTRAINT `A_ForeignID` FOREIGN KEY (`ForeignID`) REFERENCES `ForeignTable` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci
Table ForeignTable
CREATE TABLE `ForeignTable` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci
2 个解决方案
#1
1
I'd suggest to read about MySQL data integrity and FKs, then Doctrine associations, data integrity is checked by MySQL for InnodDB tables. What you're doing is not right, it should be
我建议阅读有关MySQL数据完整性和FK,然后是Doctrine关联,MySQL检查InnodDB表的数据完整性。你正在做的事情是不对的,它应该是
$entityManager->transactional(function($em) {
$foreignObject = new DoctrineEntities\ForeignTable();
$em->persist($foreignObject);
$aObject = new DoctrineEntities\A();
$aObject->setForeign($foreignObject);
$em->persist($aObject);
$em->flush();
});
#2
0
I solved the problem. It was somewhere else completly unrelated to my question.. I'm going to accept getme's answer instead because accepting this answer really won't help anyone else here...
我解决了这个问题。这是与我的问题完全无关的其他地方..我将接受getme的答案,因为接受这个答案真的不会帮助其他任何人......
#1
1
I'd suggest to read about MySQL data integrity and FKs, then Doctrine associations, data integrity is checked by MySQL for InnodDB tables. What you're doing is not right, it should be
我建议阅读有关MySQL数据完整性和FK,然后是Doctrine关联,MySQL检查InnodDB表的数据完整性。你正在做的事情是不对的,它应该是
$entityManager->transactional(function($em) {
$foreignObject = new DoctrineEntities\ForeignTable();
$em->persist($foreignObject);
$aObject = new DoctrineEntities\A();
$aObject->setForeign($foreignObject);
$em->persist($aObject);
$em->flush();
});
#2
0
I solved the problem. It was somewhere else completly unrelated to my question.. I'm going to accept getme's answer instead because accepting this answer really won't help anyone else here...
我解决了这个问题。这是与我的问题完全无关的其他地方..我将接受getme的答案,因为接受这个答案真的不会帮助其他任何人......