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


Post a Comment

Popular posts from this blog

CakeAdminLTE is based on AdminLTE Theme for CakePHP 2.4+

CakePHP: COUNT data and GROUP BY date

CakePHP: Calling function from other controller