2017-10-28 12:07:49 9228次浏览 2条回答 1 悬赏 75 金钱

如题 如何抛出自定义的异常 这样可以吗

$connection = Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
    $sql1 = 'insert into t_test1(name) values(2)';
    $sql2 = 'insert into t_test2(name) values(2)';
    $res1= $connection->createCommand($sql1)->execute();
    $res2= $connection->createCommand($sql2)->execute();
    if(!$res1){
        throw new \Exception('第一次错误');
    }if(!$res2){
        throw new \Exception('第二次错误');
    }
    // ... executing other SQL statements ...
    $transaction->commit();
} catch (\yii\db\Exception $e) {
    echo $e->getMessage();
    $transaction->rollBack();
}
补充于 2017-10-28 12:08

如果插入错误会抛出‘第一次错误’的异常 请问怎么做才能实现

您需要登录后才可以回答。登录 | 立即注册
青软实力
主管

青软实力

注册时间:2017-10-08
最后登录:2018-03-04
在线时长:9小时1分
  • 粉丝0
  • 金钱255
  • 威望20
  • 积分545

热门问题