0
我對這個問題掙扎很多。beforeSave不工作cakePHP
我的標準守則
class User extends AppModel {
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
$validate = array();
public function beforeSave($options = array()) {
parent::beforeSave();
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
}
控制器的保存方法
public function register(){
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(
__('registration is successfully completed. you can login now.'),
'success',
array(),
'auth'
);
return $this->redirect(array('action' => 'login'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.'),
'error'
);
}
$this->layout = false;
}
謝謝。
嘗試將請求數據放在create()中。或者你可以調試,如果方法被調用,但密碼不更新? – lp1051