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.
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.
I have used the following line to redirect to the view method on the newly created ‘id’:
which is inserted in the add method after the line ‘if ($this->MODEL->save($this->request->data))’:
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.