我有一个带有约束的表,该约束阻止插入某些变量组合,但是,当我从内部事务执行带有错误值的插入时,插入失败(应该如此),但PDOException
没有引发。事实上,事务就像它成功提交一样,但是下面的选择表明没有插入值(只是为了记录,使用相同的函数插入了正确的值组合)。出了什么问题?
下面是有问题的功能。db_SqlSrv::pdo()
创建并返回一个新实例PDO
,我正在使用 sqlsrv 驱动程序。
/**
* Inserts number of rules into <code>menu_availability</code> table.
* The expected format for rules is:
* <code>[{ menu_id: int,
* start: timestring,
* end: timestring,
* weekly: 0..6,
* once: datestring }, ...]</code>
*/
public static function addRules() {
$rules = web_Util::getRequestParam('rules', 'json');
if ($rules !== null) {
$pdo = db_SqlSrv::pdo();
try {
$pdo->beginTransaction();
foreach ($rules as $rule) {
$statement = $pdo->prepare(
"insert into menu_availability
(menu_id, daily_serving_start, daily_serving_end,
weekly_service_off, one_time_service_off)
values (?, ?, ?, ?, ?)");
$statement->bindParam(1, $rule->menu_id, PDO::PARAM_INT);
if ($rule->start) {
$statement->bindParam(2, $rule->start, PDO::PARAM_STR);
$statement->bindParam(3, $rule->end, PDO::PARAM_STR);
} else {
// This feels kind of stupid...
$rule->start = null;
$rule->end = null;
$statement->bindParam(2, $rule->start, PDO::PARAM_NULL);
$statement->bindParam(3, $rule->end, PDO::PARAM_NULL);
}
$statement->bindParam(4, $rule->weekly, PDO::PARAM_INT);
$statement->bindParam(5, $rule->once, PDO::PARAM_STR);
$statement->execute();
}
$pdo->commit();
} catch (PDOException $e) {
$pdo->rollBack();
throw $e;
}
}
return true;
}
表是这样定义的:
if not exists (select * from sysobjects where name = 'menu_availability' and xtype = 'U')
create table menu_availability
(menu_id int not null,
daily_serving_start time(0) null,
daily_serving_end time(0) null,
weekly_service_off tinyint null,
one_time_service_off date null,
sn as case
when ((daily_serving_start is null
and daily_serving_end is null)
and ((weekly_service_off is not null and one_time_service_off is null)
or (one_time_service_off is not null and weekly_service_off is null)))
or
((daily_serving_start is not null
and daily_serving_end is not null)
and (one_time_service_off is null
or weekly_service_off is null))
then cast(1 as bit)
end persisted not null,
constraint ch_valid_week_day
check ((weekly_service_off is null)
or (weekly_service_off <= 6 and weekly_service_off >= 0)));
应触发约束限制的示例数据:
{"menu_id":"18283","start":"","end":"","weekly":3,"once":"16-01-1901"}
weekly
(同时给出and是违法的once
)