# How to get last insert id before inserting data in cakephp 3.x?

**URL:** <https://discourse.cakephp.org/t/how-to-get-last-insert-id-before-inserting-data-in-cakephp-3-x/2597>\
**Category:** Need Help\
**Created:** [May 19, 2017, 7:06am UTC](https://discourse.cakephp.org/t/how-to-get-last-insert-id-before-inserting-data-in-cakephp-3-x/2597 "2017-05-19T07:06:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehedi094030](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mehedi094030/32/646_2.png) [@mehedi094030](https://discourse.cakephp.org/u/mehedi094030)\
**Post date:** [May 19, 2017, 7:06am UTC](https://discourse.cakephp.org/t/how-to-get-last-insert-id-before-inserting-data-in-cakephp-3-x/2597/1 "2017-05-19T07:06:24Z")

</div>

In Cake 3.x, I know the following method to get last id.

$entity = $this-\>MyModel-\>newEntity($this-\>request-\>data())  
$result= $this-\>MyModel-\>save($entity); //Statement as result  
$id = $result-\>id;

But this is only for after inserting data. Is there any way to get id before inserting data?

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [May 19, 2017, 7:11am UTC](https://discourse.cakephp.org/t/how-to-get-last-insert-id-before-inserting-data-in-cakephp-3-x/2597/2 "2017-05-19T07:11:07Z")

</div>

If the data is not inserted how you suppose to get the id, this is not possible. Btw what exactly you want to achieve? If you want some modification just before saving the entity, you can use the callbacks.

---

<div class="post-metadata">

**Author:** ![mehedi094030](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mehedi094030/32/646_2.png) [@mehedi094030](https://discourse.cakephp.org/u/mehedi094030)\
**Post date:** [May 20, 2017, 3:24am UTC](https://discourse.cakephp.org/t/how-to-get-last-insert-id-before-inserting-data-in-cakephp-3-x/2597/3 "2017-05-20T03:24:43Z")

</div>

I just want to know the last id that I used previous before saving a data. I am not sure, Is this available or not in cakephp? Anyway thanks for your reply.

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [May 22, 2017, 3:05am UTC](https://discourse.cakephp.org/t/how-to-get-last-insert-id-before-inserting-data-in-cakephp-3-x/2597/4 "2017-05-22T03:05:53Z")

</div>

Ok so do you want to get the last id which exist in table or you want the id that you are trying to save?

---

<div class="post-metadata">

**Author:** ![McScreech](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mcscreech/32/465_2.png) [@McScreech](https://discourse.cakephp.org/u/McScreech)\
**Post date:** [May 30, 2017, 4:11pm UTC](https://discourse.cakephp.org/t/how-to-get-last-insert-id-before-inserting-data-in-cakephp-3-x/2597/5 "2017-05-30T16:11:32Z")

</div>

G’day,

Setup: Windoze 7 Pro x64 SP1; Apache 2.4.23 (Win64);MySQL 5.7.15 (mysqli, mysqlnd, pdo\_mysql 5.0.12); PHP 7.0.11; CakePHP 2.9.1;

I have used the following line to redirect to the view method on the newly created ‘id’:

```auto

```

which is inserted in the add method after the line ‘if ($this-\>MODEL-\>save($this-\>request-\>data))’:

```auto
public function add() {
	if (array_key_exists('Cancel', $this->request->data)) {
		$this->Flash->error('Cancelled adding new group.');
		return $this->redirect(array('action' => 'index'));
	} elseif ($this->request->is('post')) {
		$this->Group->create();

			if ($this->Group->save($this->request->data)) {
/* this line */ $id_max = strval(max($this->Group->find('list', array('fields' => array('id')))));
			$this->Flash->success('The group has been added.');
			return $this->redirect(array('action' => 'view', $id_max));
		} else {
			$this->Flash->error('The group could not be added! Please, try again.');
		}
	}
	return $this->render('edit');
} // end add()
```

I use the maximum value found because I have intermediate ‘id’ indices missing due to record deletions over time, so the count(‘id’) value is always (in MY case) too low.

McS
