CakePHP : Updating the session user data after edit


<?php  
//users controller
 
if ($this->User->save($this->request->data)) {
                if($this->request->data['User']['id'] == AuthComponent::User('id')){ //if current logged in user
  update user session data

                  $this->Session->write('Auth.User.first_name', $this->request->data['User']['first_name']); //updating first_name only   

                  $this->Session->write('Auth.User.username', $this->request->data['User']['username']); //updating username only             

                  $this->Session->write('Auth.User', array_merge(AuthComponent::User(), $this->request->data['User']) );   //updating all user session data

                }
                $this->Session->setFlash('The user has been saved');
                $this->Session->setFlash('', 'default', array(), 'success');
                $this->redirect(array('action' => 'index'));
    } else {
            $this->Session->setFlash('The user could not be saved. Please, try again.');
    } 

?>

Learn more about Interacting with session data here: http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html


Comments