Just make a PRG template .. Is it really that simple? Well, at least what everyone says, but no one gives a clear answer! It took me a week to search and dig, and then Novice decided to do something on my own! Here is one way to do this in cakephp (I am using 2.0.5):
Regardless of the code, there is logic in the steps:
1- given data
2- confirm (do not create yet ())
3- write $ this-> request-> data to the session variable
4- redirect to saveData action p>
Inside saveData action:
5- read and save the session variable
6 - DELETE session variable
7-create ()
8 - saving data in the model
9-redirection
Here is an example of what your code might look like.
** Attn: " our control " and ourModel "
public function add() { if ($this->request->is('post')) { if (isset($this->request->data)) { $this->ourModel->set($this->request->data); if ($this->ourModel->validates()) { $this->Session->write('myData', $this->request->data); $this->redirect(array('controller' => 'ourController', 'action' => 'saveData', 'ourModel'
Then you should redirect (when checking) to this function, which redirects you again to the indexing action or wherever your logic takes you!
public function saveData($model) { $myData = $this->Session->read('myData'); $this->Session->delete('myData'); //extremely important $this->$model->create(); if ($this->$model->save($myData)) // or $myData[$model] if you are dealing with multiple models { $this->Session->setFlash(__($model.' have been saved successfully')); $this->redirect(array('controller' => 'ourController', 'action' => 'index' ) ); } } else{ $this->Session->setFlash(__($model.' could not be saved')); } } }
A simple self-redirect may work, but in most cases you want to redirect to a different view (for example, to a different form or to an index view).
I hope that this development will save time on others, so you do not need to spend a whole week (as in my case) to make such functionality on the server side!
source share