Christian Strang
Christian Strang

Reputation: 8530

Cakephp 2.1 beforeSave not working

I have a User Model, a UsersController and an AccountController which uses the User Model (the account controller is used when an account is created, login, logout).

Everything works fine, except the beforeSave function in AccountController. I'm trying to use beforeSave to hash my password but it doesn't work (the password is saved un-hashed in the database).

public function beforeSave() {
    parent::beforeSave();   

    if (isset($this->request->data['User']['password'])) {
        $this->request->data['User']['password'] = sha1($this->request->data['User']['password']);
    }

    return true;
}

A few notes:

I think in my case beforeSave is not being called, I just can't figure out why.


Solved: The beforeSave function has to go inside the model, this is my beforeSave function now:

public function beforeSave($options = array()) {
        parent::beforeSave();
        $this->data['User']['password'] = sha1($this->data['User']['password']);
        return true;
    }

Upvotes: 4

Views: 5787

Answers (1)

Joep
Joep

Reputation: 651

beforeSave is a Model callback, so define it in your Model(s).

Upvotes: 3

Related Questions