Skip to content

Commit 5d7007a

Browse files
committed
Enhance documentation for ActiveRecord::transact and ActiveRecord::groupActions methods
1 parent 23a0e49 commit 5d7007a

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

docs/en/active-record/active-entity.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -329,23 +329,30 @@ To achieve this, you can use two methods: `::transact()` and `::groupActions()`.
329329
The `::transact()` method will immediately open a transaction and complete it along with the callback.
330330
The transaction will be rolled back if an exception is thrown from the function; otherwise, it will be committed.
331331

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.
335338

336339
```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');
340343

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
342345
$user->save();
343-
// Will be executed in a nested transaction
344-
$account->save();
345-
// Will be executed in a nested transaction
346+
$account->saveOrFail();
346347
$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;
347353
}
348354
);
355+
echo $result; // 42
349356
```
350357

351358
### ActiveRecord::groupActions()
@@ -357,8 +364,8 @@ The `::groupActions()` method differs from `::transact()` in that:
357364

358365
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.
359366

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:
362369

363370
```php
364371
ActiveRecord::groupActions(
@@ -380,10 +387,10 @@ The `TransactionMode` enum provides the following options:
380387
it won't be committed or rolled back.
381388

382389
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.
384391

385392
```php
386-
User::transact(function (DatabaseInterface $dbal) use ($user, $account, $post): void {
393+
User::transact(function (DatabaseInterface $dbal, EntityManagerInterface $em) use ($user, $account, $post): void {
387394
$dbal->query('DELETE FROM users');
388395

389396
User::groupActions(function () use ($user, $account, $post) {

0 commit comments

Comments
 (0)