2 Comments

  1. Your approach to edit own profile is an awesome hack. Thanks!

    Maybe for version difference reasons your code didnt work. Am on CakePHP 3.0. My solution:

    > Controller:

        public function editme() {
            $uid = $this->request->session()->read('Auth.User.id');
            $user = $this->Users->get($uid, []);
            if ($this->request->is(['patch', 'post', 'put'])) {
                $user = $this->Users->patchEntity($user, $this->request->data);
                if ($this->Users->save($user)) {
                    $this->Flash->success(__('Your account changes have been saved.'));
                    $this->redirect('/users/view/'.$user['id']);
                } else {
                    $this->Flash->error(__('Something went wrong and your changes were not saved. Try again.'));
                }
            }
    
            $this->set(compact('user'));
            $this->set('_serialize', ['user']);
        }

    View template:

    Form->create($user); ?>
        
            
            Form->input('first_name');
                echo $this->Form->input('last_name');
                echo $this->Form->input('email');
                echo $this->Form->input('password');
                echo $this->Form->hidden('active', [
                  'default' => '1'
                ]);
            ?>

    Thanks!

  2. im currently working on my cakephp app i have database named time and is has to table users and accounts users have EployeeId, Password data fields and accounts has also EmployeeId , Password etc.. now what i want to happen is every time that i will add data to users table it will authenticate if that data to save is present in the accounts table if not it will flash error else it will save how can i do this in cakephp ?? here is may

    add.ctp code

    public function add()
    {
    $user = $this->Users->newEntity();
    if ($this->request->is(‘post’)) {
    $user = $this->Users->patchEntity($user, $this->request->data);
    if ($this->Users->save($user)) {
    $this->Flash->success((‘Logs has been saved.’));
    return $this->redirect([‘action’ => ‘index’]);
    } else {
    $this->Flash->error(
    (‘The logs could not be saved. Please, try again.’));
    }
    }
    $this->set(compact(‘user’));
    $this->set(‘_serialize’, [‘user’]);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.