# Get Last insert id

**URL:** <https://discourse.cakephp.org/t/get-last-insert-id/8649>\
**Category:** Need Help\
**Created:** [November 22, 2020, 3:55pm UTC](https://discourse.cakephp.org/t/get-last-insert-id/8649 "2020-11-22T15:55:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal-alnaeb](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jamal-alnaeb](https://discourse.cakephp.org/u/jamal-alnaeb)\
**Post date:** [November 22, 2020, 3:55pm UTC](https://discourse.cakephp.org/t/get-last-insert-id/8649/1 "2020-11-22T15:55:06Z")

</div>

Good evening. Appreciate any effort to help me.  
I use version 4 and I need to get the last insert id after I save the row in the database from the controller, in previous versions, there was a function  
$orderId = $this-\>Orders-\>getLastInsertId();  
in this version, it gives me a mistake,

Thank you

---

<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:** [November 22, 2020, 8:07pm UTC](https://discourse.cakephp.org/t/get-last-insert-id/8649/2 "2020-11-22T20:07:30Z")

</div>

It will have been added to whatever entity you just saved. But why do you need it? If it’s to add to other related entities, then you can save them all at once and Cake will take care of that for you.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [November 23, 2020, 6:12pm UTC](https://discourse.cakephp.org/t/get-last-insert-id/8649/3 "2020-11-23T18:12:02Z")

</div>

> if ($articlesTable-\>save($article)) {  
> // The $article entity contains the id now  
> $id = $article-\>id;  
> }

taken from:  
[https://book.cakephp.org/4/en/orm/saving-data.html](https://book.cakephp.org/4/en/orm/saving-data.html)

---

<div class="post-metadata">

**Author:** ![jamal-alnaeb](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jamal-alnaeb](https://discourse.cakephp.org/u/jamal-alnaeb)\
**Post date:** [November 25, 2020, 4:48pm UTC](https://discourse.cakephp.org/t/get-last-insert-id/8649/4 "2020-11-25T16:48:21Z")

</div>

it is useful,  
Thank you very much
