Transaction on multiple models

How can I perform single transaction on multiple models in cakephp 3.8?
My code:
$conn->begin();
TableRegistry::getTableLocator()->get(‘Model 1’)->save();
TableRegistry::getTableLocator()->get(‘Model 2’)->update();
TableRegistry::getTableLocator()->get(‘Model 3’)->delete();
$conn->commit();

That looks like exactly how you would do it. Is there some problem?

Yes, even if the second execution doesn’t takes place the transaction doesn’t get rollbacked.

There is nothing in your code that would prevent the commit from being called. You presumably need to add something that checks for failure of the various operations and skips the rest in such cases. Personally, I always use the transactional function for such things, as it makes it easier to deal with; you just return false from the function early to roll back the transaction, instead of needing some increasing depth of nested if statements.

save() return false on error, you can use saveOrFail() if you want an exception and trigger rollback