You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/active-record/active-entity.md
+21-14Lines changed: 21 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -329,23 +329,30 @@ To achieve this, you can use two methods: `::transact()` and `::groupActions()`.
329
329
The `::transact()` method will immediately open a transaction and complete it along with the callback.
330
330
The transaction will be rolled back if an exception is thrown from the function; otherwise, it will be committed.
331
331
332
-
Note that this does not currently affect the execution of ActiveRecord write operations.
333
-
Single saves or deletions of entities will still be executed in their own transactions.
334
-
This behavior may change in the future.
332
+
All the ORM operations within the callback will be executed in the opened transaction without collecting.
333
+
If you need to collect ORM operations and execute them in a separated inner transaction,
334
+
use `::groupActions()` within the callback.
335
+
336
+
If you call this method from a child class, the child database connection will be used for the transaction.
337
+
If you call this method from the `ActiveRecord` class, the default database connection will be used.
335
338
336
339
```php
337
-
ActiveRecord::transact(
338
-
function () use ($user, $account, $post) {
339
-
$dbal->query('DELETE FROM users');
340
+
$result = ActiveRecord::transact(
341
+
static function (DatabaseInterface $db, EntityManagerInterface $em) use ($user, $account, $post): int {
342
+
$db->query('DELETE FROM users');
340
343
341
-
// Will be executed in a nested transaction
344
+
// All the ORM actions will be executed right away in the opened transaction using isolated UoW
342
345
$user->save();
343
-
// Will be executed in a nested transaction
344
-
$account->save();
345
-
// Will be executed in a nested transaction
346
+
$account->saveOrFail();
346
347
$post->delete();
348
+
349
+
// EM executes action right away, you don't need to call $em->run()
350
+
$em->persist(new Post('Title', 'Content'));
351
+
352
+
return 42;
347
353
}
348
354
);
355
+
echo $result; // 42
349
356
```
350
357
351
358
### ActiveRecord::groupActions()
@@ -357,8 +364,8 @@ The `::groupActions()` method differs from `::transact()` in that:
357
364
358
365
In other words, all operations on entities within the function are placed into a single Unit of Work of the EntityManager and executed upon exiting the function.
359
366
360
-
You can get the EntityManager as the first parameter of the passed function to perform actions with other ORM entities that are not converted to ActiveRecord.
361
-
To configure the transaction behavior of the Entity Manager, you can use the second parameter of the `transact()` method:
367
+
You can get the EntityManager as the first parameter of the passed function to perform actions with other ORM entities that are not converted to ActiveRecord.
368
+
To configure the transaction behavior of the Entity Manager, you can use the second parameter:
362
369
363
370
```php
364
371
ActiveRecord::groupActions(
@@ -380,10 +387,10 @@ The `TransactionMode` enum provides the following options:
380
387
it won't be committed or rolled back.
381
388
382
389
You can nest the call to `::groupActions()` inside the call to `::transact()` to avoid creating unnecessary nested transactions.
383
-
However, nesting `::groupActions()`inside another `::groupActions()` is not currently allowed.
390
+
Nested calls to `::groupActions()`will use separated Unit of Works, but transactions will be reused according to the given mode.
384
391
385
392
```php
386
-
User::transact(function (DatabaseInterface $dbal) use ($user, $account, $post): void {
0 commit comments