# Transaction on multiple models

**URL:** <https://discourse.cakephp.org/t/transaction-on-multiple-models/8552>\
**Category:** Need Help\
**Created:** [October 28, 2020, 7:32am UTC](https://discourse.cakephp.org/t/transaction-on-multiple-models/8552 "2020-10-28T07:32:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [October 28, 2020, 7:32am UTC](https://discourse.cakephp.org/t/transaction-on-multiple-models/8552/1 "2020-10-28T07:32:12Z")

</div>

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();

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 28, 2020, 1:12pm UTC](https://discourse.cakephp.org/t/transaction-on-multiple-models/8552/2 "2020-10-28T13:12:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [January 6, 2021, 5:38am UTC](https://discourse.cakephp.org/t/transaction-on-multiple-models/8552/3 "2021-01-06T05:38:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 6, 2021, 3:12pm UTC](https://discourse.cakephp.org/t/transaction-on-multiple-models/8552/4 "2021-01-06T15:12:06Z")

</div>

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](https://book.cakephp.org/3/en/orm/database-basics.html#using-transactions) 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.

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [January 6, 2021, 8:35pm UTC](https://discourse.cakephp.org/t/transaction-on-multiple-models/8552/5 "2021-01-06T20:35:57Z")

</div>

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