Reset object (or create a new empty object) in the controller after saving

How do you reset an object after saving if there is no redirect after saving (if you stay on the same activity / page)?

Example. I have a page that lists all users. On the same page there is a form for adding a new user.

public function index() {
   $this->set('listallusers', $this->Users->find('all'));
   $user = $this->Users->newEntity();
   if ($this->request->is('post')) {
     $user = $this->Users->patchEntity($user, $this->request->data );
     if ( $this->Users->save($user) ) {
       $this->Flash->succeed('User saved.');
       // line 8
     } else {
       $this->Flash->error('There is an error.');
     }
   }
   $this->set('user', $user);
 }

      

I tried adding $user = '';

after the flash message on line 08 as well$user = $this->Users->newEntity();

But the form on the page still contains / shows the values ​​of the last saved user (I want to have an empty form when the save was successful).

+1


source to share


1 answer


I don't know how to mark a comment as an answer to my question, so I am quoting it:

You don't just stay on the same page. The save is the result of a POST, so you need to do a GET (redirect) to follow the PRG pattern, which you should follow for several reasons to avoid other problems. Then your question is decided by itself. Therefore, ALWAYS redirect in your case only to the same URL again. Problem solved - by design. - Mark



Thanks @mark

0


source







All Articles