yii2 事务

$transaction = Yii::$app->db->beginTransaction();
try{
    //删除$model中的数据
    $res = $model->deleteAll($cond);
    if(!$res)
        throw new \Exception('操作失败!');
    
    //删除$model对应的$relation中的数据
    $rt = $relation->deleteAll(['polymeric_id'=>$cond['id']]);
    if(!$rt)
        throw new \Exception('操作失败!');
    
    //以上执行都成功,则对数据库进行实际执行
    $transaction->commit(); 
    return Helper::arrayReturn(['status'=>true]);
}catch (\Exception $e){
    //如果抛出错误则进入catch,先callback,然后捕获错误,返回错误
    $transaction->rollBack();
    return Helper::arrayReturn(['status'=>false,'msg'=>$e->getMessage()]);
}
posted @ 2017-05-25 16:54  pengcx  阅读(561)  评论(0编辑  收藏  举报