Error when updating the roles of my users

Good morning guys, best regards.
I am writing to you this time so that you can guide me in the following question.
in my edit controller I capture the role of my users by the url, every time I edit a user the role remains the same, but this time I need to change said role or role_id.
everything works correctly except the role_id, I share part of my code for better understanding


public function edit($id = null)
        $this->set('title', __('Edit User'));

        $roleuserlogged = $_SESSION['Auth']['User']['role_id'];

        if ($this->request->is(['patch', 'post', 'put'])) { 
            $user = $this->Users->patchEntity($user, $this->request->getData(),['associated'=>['Central']]);
            if ($this->Users->save($user,['associated'=>['Central']])) {

                if($user->role_id == Configure::read('ROLES.KIPPERO_NOCERTF') && $user->active == false){
                    $user->role_id = Configure::read('ROLES.KIPPERO_NOCERTF');
                    $role = 'kippero_nocertf';
                    $this->Flash->success(__('The kippero has been saved.')); 

                if ($user->role_id == Configure::read('ROLES.KIPPERO_NOCERTF') && $user->active == true) {
                    $user->role_id = Configure::read('ROLES.KIPPERO');
                    $role = 'kipperos';
                    $this->Flash->success(__('The kippero has been saved.')); 
                return $this->redirect(['action' => 'index','role' => $role]);
            $this->Flash->error(__('The user could not be saved. Please, try again.'));

        $cities = $this->Users->Cities->find('list', ['limit' => 200])
                                ->order(['name' => 'asc']);

        $roles = $this->Users->Roles->find('list', ['limit' => 200])
                                ->order(['name' => 'asc']);

        if ($roleuserlogged == Configure::read('ROLES.SUPERADMIN')) {
            $central = $this->Users->Central->find('list', ['limit' => 200])
                                ->order(['name' => 'asc']);
            $central = $this->Users->Central->find('list', ['limit' => 200])
                                ->where([ 'id' =>$_SESSION['Auth']['User']['central_id'] ])
                                ->order(['name' => 'asc']);
        $this->set(compact('user', 'cities', 'roles', 'central'));


<!--div class="col-md-2">
                    <div class="form-group">
                        <label><?= __('Role') ?></label-->                        
                        <?php //echo $this->Form->hidden('role', ['label' => false, 'class' => 'form-control']);
                           echo $this->Form->hidden('role', ['id'=>'role', 'value' => $role]);  
                            /* echo $this->Form->hidden('role', ['id'=>'role', 'value' => $role_id]);*/

I appreciate any guidance on this.

Just setting the role_id property on the $user php object doesnt save it automatically.
You have to call $this->Users->save($user); again to actually save the value

hello, thank you for your guidance. What I did was the following:

set the property $user->role_id

before calling


according to my logic in this way I establish the condition before saving/updating that specific role_id

and it worked perfectly.

Thank you.