analogueorm / analogue

Analogue ORM : Data Mapper ORM for Laravel/PHP
MIT License
632 stars 51 forks source link

How to use custom transactions? #140

Closed vovanmix closed 7 years ago

vovanmix commented 7 years ago

Hi! I need to use custom transaction to wrap multiple write/update operations on different non-related tables. How can I do that using this ORM? Thank you!

RemiCollin commented 7 years ago

Hello! Laravel transactions are nestable, so you can simply embed the operations in a database transaction closure :

DB::transaction(function () {
   // Multiple write / update operation 
});

Or manually start / commit transaction :

DB::beginTransaction();

// Write operations

DB::commit();
vovanmix commented 7 years ago

Hi! The thing is that I'm not using Laravel, and I use your ORM as a only gateway to the database.

RemiCollin commented 7 years ago

Analogue is based on Illuminate/Database, so these functions are available all along (yet syntax may differ, as the examples aboves are using facades). http://stackoverflow.com/questions/26490671/how-can-i-implement-transactions-with-laravels-illuminate-database-class

vovanmix commented 7 years ago

Can you by any chance show an example on how to call it from an instance of the orm? Assuming that I have this instance in $db, what do I call? I don't use facades and it seems weird to include another depencency just to call a transaction.

RemiCollin commented 7 years ago

Simply call $db->beginTransaction() , should work