{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/9851","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/9851/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/9851/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/9851/events","html_url":"https://github.com/yiisoft/yii2/issues/9851","id":110360914,"number":9851,"title":"Ability to rollback a closure-style transactions","user":{"login":"Vovan-VE","id":1166581,"avatar_url":"https://avatars.githubusercontent.com/u/1166581?v=3","gravatar_id":"","url":"https://api.github.com/users/Vovan-VE","html_url":"https://github.com/Vovan-VE","followers_url":"https://api.github.com/users/Vovan-VE/followers","following_url":"https://api.github.com/users/Vovan-VE/following{/other_user}","gists_url":"https://api.github.com/users/Vovan-VE/gists{/gist_id}","starred_url":"https://api.github.com/users/Vovan-VE/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Vovan-VE/subscriptions","organizations_url":"https://api.github.com/users/Vovan-VE/orgs","repos_url":"https://api.github.com/users/Vovan-VE/repos","events_url":"https://api.github.com/users/Vovan-VE/events{/privacy}","received_events_url":"https://api.github.com/users/Vovan-VE/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:enhancement","name":"type:enhancement","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/yiisoft/yii2/milestones/9","html_url":"https://github.com/yiisoft/yii2/milestones/2.0.x","labels_url":"https://api.github.com/repos/yiisoft/yii2/milestones/9/labels","id":831720,"number":9,"title":"2.0.x","description":"","creator":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"open_issues":418,"closed_issues":3,"state":"open","created_at":"2014-10-19T13:49:45Z","updated_at":"2015-10-08T10:40:13Z","due_on":null,"closed_at":null},"comments":3,"created_at":"2015-10-08T02:34:54Z","updated_at":"2015-10-08T16:18:27Z","closed_at":null,"body":"Closure-style transaction shortcut is much readable in easy to use, then full code. But closure-style does not allow an easy rollback. The only whay is to `throw` and `catch` custom exception. In that way it will be a mess of that two styles:\r\n\r\n```php\r\ntry {\r\n    \\Yii::$app->db->transaction(function () {\r\n        if (something was wrong) {\r\n            throw new MyRollbackException();\r\n        }\r\n    });\r\n    // done\r\n} catch (MyRollbackException $e) {\r\n    // rolled back\r\n}\r\n```\r\n\r\nIt whould be very nice to have ability to make rollback more easy. For example, it can be something like this:\r\n\r\n```php\r\n\\Yii::$app->db->transaction(function () {\r\n    if (something was wrong) {\r\n        throw new \\yii\\db\\RollbackException();\r\n    }\r\n});\r\n```\r\n\r\nBut the problem is what to do with return value in case of rollback.\r\nMay be\r\n\r\n```php\r\n$result = \\Yii::$app->db->transaction(function () {\r\n    throw new RollbackException(42);\r\n});\r\n// $result === 42\r\n```\r\n\r\nor one more closure, which will be called in case of `RollbackException` and which return value will be used in that case:\r\n\r\n```php\r\n// another method to keep BC\r\n$result = \\Yii::$app->db->transaction2(\r\n    function () {\r\n        throw new RollbackException();\r\n    },\r\n    function (RollbackException $e) {\r\n        return 42;\r\n    }\r\n);\r\n// $result === 42\r\n```"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/146599383","html_url":"https://github.com/yiisoft/yii2/issues/9851#issuecomment-146599383","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/9851","id":146599383,"user":{"login":"SamMousa","id":547021,"avatar_url":"https://avatars.githubusercontent.com/u/547021?v=3","gravatar_id":"","url":"https://api.github.com/users/SamMousa","html_url":"https://github.com/SamMousa","followers_url":"https://api.github.com/users/SamMousa/followers","following_url":"https://api.github.com/users/SamMousa/following{/other_user}","gists_url":"https://api.github.com/users/SamMousa/gists{/gist_id}","starred_url":"https://api.github.com/users/SamMousa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SamMousa/subscriptions","organizations_url":"https://api.github.com/users/SamMousa/orgs","repos_url":"https://api.github.com/users/SamMousa/repos","events_url":"https://api.github.com/users/SamMousa/events{/privacy}","received_events_url":"https://api.github.com/users/SamMousa/received_events","type":"User","site_admin":false},"created_at":"2015-10-08T16:18:27Z","updated_at":"2015-10-08T16:18:27Z","body":"Commit and rollback support nesting just fine, I don't see how this would break it."}}