我在数据库中有以下结构:
在我的代码中,我首先创建了一个公司,然后是一个日志,然后是一些时间表,然后是一个用户,然后是一个日志责任。有点像这样(不是为了简洁起见的确切代码,但我认为这并不重要:
<?php
$company = new Company();
$company->setSomething(123);
$logbook = new Logbook();
$logbook->setSomething('abc');
$logbook->setCompany($company);
$schedules = array();
for ($x=0; $x<$something; $x++) {
$schedule = new Schedule();
$schedule->setSomething(doSomethingWithSomething($something[$x]);
$schedule->setLogbook($logbook);
$schedules[] = $schedule;
}
$user = new User();
$user->setSomething('something');
$user->setCompany($company);
$user->setCurrentLogbook($logbook);
$logbookResponsibility = new LogbookResponsibility();
$logbookResponsibility->setLogbook($logbook);
$logbookResponsibility->setResponsibilityId(1);
$logbookResponsibility->setUser($user);
$errors = someFormOfCheck();
if (!$errors) {
$user->save();
$logbookResponsibility->save();
foreach ($schedules as $schedule) {
$schedule->save();
}
}
我收到以下错误:
<b>Fatal error</b>: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`logbook`.`logbook_responsibility`, CONSTRAINT `fk_logbook_responsbility_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON UPDATE CASCADE)' in C:\wamp\bin\php\php5.3.8\pear\propel\connection\DebugPDOStatement.php:90
该logbook_responsibility
表由三个外键组成,每个外键都有一个RESTRICT
更新,因为如果存在日志责任,我不希望删除用户、日志或责任类型。但是,我相信问题是由级联的顺序决定的。
如果我完全取出$logbookResponsibility
零件,它会完美运行。如果我将这些$logbookResponsibility
部分放在之后,$user->save();
然后再发出 a $logbookResponsibility->save();
,那也很有效。
那么,我的问题是,我哪里出错了?为了使级联工作,我在假设不正确或做错了什么?还是有其他可能阻止它的东西?
更新:如果我执行以下操作,它工作得很好:
$company->save();
$logbook->save();
$user->save();
$logbookResponsibility->save();
foreach ($schedules as $scheudle) {
$schedule->save();
}
即,如果我先手动保存它们而不是依赖级联。