PHP学院的中学生 2019-03-06 15:49:36 1557次浏览 0条回复 0 0 0
//删除profile 和 user 表中的各一条记录,如果未报异常,则提交,如果出现异常,则进行回滚,此次操作无效

try{
    $userid = (int)Yii::$app->request->get('userid');
    if (empty($userid)) {
        throw new \Exception();
    }
    $trans = Yii::$app->db->beginTransaction();//开始事务
    if ($obj = Profile::find()->where('userid = :id', [':id' => $userid])->one()) {
        $res = Profile::deleteAll('userid = :id', [':id' => $userid]);
        if (empty($res)) {
            throw new \Exception();
        }
    }
    if (!User::deleteAll('userid = :id', [':id' => $userid])) {
        throw new \Exception();
    }
    $trans->commit();
} catch(\Exception $e) {
    if (Yii::$app->db->getTransaction()) {
        $trans->rollback();//回滚
    }
}
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册