Hi, I would like mention some advantages of developing code within the so-called transaction, which is available in EspoCRM via Transaction Manager.
What is a transaction in terms of development?
- The answer is very simple. Transaction is a "container", or "set" of logic operations, which are executed one after another with let's say "promise" to complete all the requests successfully, or not complete any request at all.
So basically it's "all or nothing".
Why would you want to use transaction in EspoCRM?
Let's say you have a complex logic in code, something like:
1) Retrieve related record
2) List all related entites via 1:N relationship
3) Foreach related entity perform retrieve of related entity
4) For each entity call external API and retrieve some value and set it on the entity
5) Set the value to that entity in EspoCRM
6) Set value on previous entity
etc...
Let's say this logic is more complex than just retrieve value and set value on a simple record.
If you would use common way to approach it, you would simply create a afterSave hook for example, that would fire the 1), then 2) etc.
The main difference between transaction and the common way is, that if you will not use transaction in this logic and the code fails at 5) or 6), the code before it will still be executed.
On the other hand, if you would use transcation, and the code would fail even on 20) for example, all the actions performed before 20) would be rollbackem as it would never been executed. All or nothing -> either all the logic of the code will be successfull, or everything will be rollbacked and not a single action will be performed.
Mostly the transactions are used when you have a bit more complex code and you are retrieving and mainly settings the values on more records etc. -> because sometimes it doesn't make sense to set some value, if the code fails in next step of setting some other value to related record etc.
On the other hand, I like to use transactions almost every time, because I see the code as a logical container of business requirements and usually all the steps in code are important, so I tend to use the transaction a lot, because I want all the logic of the code be sucessfull, or rollback everything, log error and solve the issue etc.
How to use transaction?
Very simple, just use the transaction manager in you code, something like:
More ways how to use the transactions: https://docs.espocrm.com/development...action-manager
What is a transaction in terms of development?
- The answer is very simple. Transaction is a "container", or "set" of logic operations, which are executed one after another with let's say "promise" to complete all the requests successfully, or not complete any request at all.
So basically it's "all or nothing".
Why would you want to use transaction in EspoCRM?
Let's say you have a complex logic in code, something like:
1) Retrieve related record
2) List all related entites via 1:N relationship
3) Foreach related entity perform retrieve of related entity
4) For each entity call external API and retrieve some value and set it on the entity
5) Set the value to that entity in EspoCRM
6) Set value on previous entity
etc...
Let's say this logic is more complex than just retrieve value and set value on a simple record.
If you would use common way to approach it, you would simply create a afterSave hook for example, that would fire the 1), then 2) etc.
The main difference between transaction and the common way is, that if you will not use transaction in this logic and the code fails at 5) or 6), the code before it will still be executed.
On the other hand, if you would use transcation, and the code would fail even on 20) for example, all the actions performed before 20) would be rollbackem as it would never been executed. All or nothing -> either all the logic of the code will be successfull, or everything will be rollbacked and not a single action will be performed.
Mostly the transactions are used when you have a bit more complex code and you are retrieving and mainly settings the values on more records etc. -> because sometimes it doesn't make sense to set some value, if the code fails in next step of setting some other value to related record etc.
On the other hand, I like to use transactions almost every time, because I see the code as a logical container of business requirements and usually all the steps in code are important, so I tend to use the transaction a lot, because I want all the logic of the code be sucessfull, or rollback everything, log error and solve the issue etc.
How to use transaction?
Very simple, just use the transaction manager in you code, something like:
PHP Code:
... constructors, override methods etc..
public function doMyLogic(EntityManager $entityManager, Logger $logger)
{
$transactionManager = $entityManager->getTransactionManager();
$transactionManager->start();
try {
// 1)
// 2)
// 3)
// etc..
$transactionManager->commit();
}
catch (Exception $ex)
{
logger.logError($ex->getMessage());
$transactionManager->rollback();
}
}
Comment