保存后在控制器中重置实体(或创建新的空实体)

Reset an entity (or create a new empty entity) in controller after saving

如果保存后没有重定向(如果保持原样 action/page),如何在保存后重置实体?

示例:我有一个列出所有用户的页面。在同一页面上有用于添加新用户的表单。

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);
 }

我试着在第08行的flash message后面加上$user = '';,还有$user = $this->Users->newEntity();

但是页面上的表格仍然是containing/showing上次保存的用户的值(我希望保存成功时有一个空表格)。

我不知道如何将评论标记为我问题的答案,所以我引用它:

You don't just stay on the same page. A save is the result from a POST, thus you need to make a GET (redirect) afterwards to follow the PRG pattern, which you must follow for several reasons to avoid other issues coming up. Then your issue resolves itself. So ALWAYS redirect, in your case just to the same URL again. Problem solved - by design. – mark

谢谢@mark