-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveRecord: transaction support #226
Comments
If do this change
For me too, happens in every application. |
OK, preparing PR to be more convincing. :-) |
btw, guys would it be some auto-transaction for db-component maybe, i think it is useful? |
@Ragazzo what do you mean? |
I mean that if i set something like this: //in config for example
components => array(
'db'=> array(
'dsn' => '...',
'user'=> '...',
'charset' => 'utf-8',
'autoTransactional' => 'true',
),
), then all queries for insert/delete in this connection will be in one transaction, and will only applied on the application end. can be useful in some cases, but dont know how to deal with select queries in that way. There were some requests for this feature in russian forums, but in Yii1.1 it is hard to implement and need to think how to do this, simple onBeginRequest/onEndRequest i think will not be enough. |
I don't think there's any sense in wrapping single queries in transactions. |
@Ragazzo Looks are not only useless, but also will be very disturbed. |
PHPDocs (inside appropriate pull request) are also explains my proposal. :-) |
@Ragazzo, it somehow reminds me Rails3 sandboxed environment/console. Not that meaningless as for me. |
Can't this be implemented by attaching to onBeginRequest() / onEndRequest() and starting / committing transactions there? |
@resurtm yes, "idea" was taken from there. just thoughts, if this can be useful, anyway. |
@creocoder, to be even more pedantic it (@bwoester's sentence) could be achieved through nested transactions/savepoints (RDBMS specific though). But in my humble opinion i see no real benefits of that too. :-) |
We're going to implement transactions support on Connection level. Not sure what's the benefit on doing it at AR level. |
I'm not quite convinced with this approach. Note that only the AR model itself knows whether transaction should be used when saving/deleting a record of it. With this So my opinion is that if an AR saves some additional related records in |
Yes, this second approach is better. It still has the following problems though:
|
Interesting results after ten minutes of googling:
However i think that one transaction is enough to wrap together both outer (controller, component, whatever) and inner (model) DB operations in most cases (85—90 % of them). Interesting to know the other developers' experience on emulating nested transactions through savepoints (mostly for MySQL, PgSQL and SQLite3).
Yes and for this case i've made outer transaction detection. :-)
I'll enhance my PR in a moment. |
Fixes #226: atomic operations and transaction support in AR.
Consider we've designed the following AR model:
User::$postIds
property used in a view form in checkbox list field. Note thatUser::save()
method was slightly enhanced. On what basis this decision made?1. We could work with transactions inside controller action, but this approach violates fat model—thin controller principle. Everyone agree we shouldn't deal with the database transactions inside controller layer? This way is conceptually incorrect.
2. Create new abstraction layer (behavior, helper, whatever) which would deal with the related records and it could use database transactions to save main AR model with related AR models atomically. I don't think i have to introduce additional abstraction layer whilst it can be solved in a more lightweight fashion.
3. Enhance AR class itself. I did it in my own AR model but i propose to do this in
yii\db\ActiveRecord
as well. The solution would be to introduce third parameter (called something like$wrapWithTransaction
) inActiveRecord::insert()
,ActiveRecord::update()
andActiveRecord::save()
methods.What do you think about my proposal? Patch itself is straightforward and ready to be merged.
I can say that this situation happens to me very often and it worth implementing it in the core.
The text was updated successfully, but these errors were encountered: