0

我有一个带有约束的表,该约束阻止插入某些变量组合,但是,当我从内部事务执行带有错误值的插入时,插入失败(应该如此),但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

4

1 回答 1

0

PDO 要求您明确告诉它在发生错误时抛出异常。默认情况下,它不会触发错误,也不会抛出异常,它希望您使用PDO::errorCode()and手动检查错误PDO::errorInfo(),以及它们的等价物 on PDOStatement

通过调用PDO::setAttribute()您可以更改此行为,通常我在设置 PDO 连接时执行此操作:

$db = new PDO($myDSN);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

执行此操作时,PDO 将在遇到错误时抛出异常。

还值得注意的是,无论此设置如何,连接错误总是会引发异常,因为连接是在构造函数中建立的,而异常实际上是处理此问题的唯一方法 - 您显然不能调用尚未调用的对象的方法被实例化。这是构造函数通常不应包含业务逻辑的原因之一的一个很好的例子,它在这种情况下大大降低了灵活性。

于 2013-03-07T23:34:12.413 回答