CakePHP - registration


CakePHP - registration

我正在制作一个小型的cakephp网站。我唯一的问题是,注册不工作后修复编辑用户留下空白的密码字段。我尝试了很多选择,但我的注册仍然不起作用。下面是我的代码:

//User.php - Model

public function beforeSave($options = array()) {
        parent::beforeSave($options);
        if (!empty($this->data[$this->alias]['pwd'])) {
            $PasswordHasher = new SimplePasswordHasher(); 
            $this->data[$this->alias]['password'] = $PasswordHasher->hash($this- >data[$this->alias]['pwd']);
        }
        return true;
    }

//UsersController.php - Controller (part with registration)

public function register() {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < 10; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    $this->set('password', $randomString);
    if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->User->save($this->request->data, true, array('username', 'mail', 'firstName', 'lastName', 'pwd', 'pwd_confirmation'))) {
            $this->Session->setFlash('Rejestracja przebiegła pomyślnie!');
            return $this->redirect(array('controller' => 'pages', 'action' => 'home'));
        }
        $this->Session->setFlash('Rejestracja nie powiodła się, spróbój jeszcze raz.');
        unset($this->request->data['User']['pwd']);
        unset($this->request->data['User']['pwd_confirmation']);
    }
}

//注册。ctp - View with register form

echo $this->Form->create('User', array('action'=>'register'));
 echo $this->Form->input('username', array('label'=>'Nazwa użytkownika'));
 echo $this->Form->input('pwd', array('label'=>'Hasło - należy skopiować', 'type'=>'text', 'value'=>$password, 'disabled' => 'disabled', 'autocomplete'=>'off'));
 echo $this->Form->input('pwd_confirmation', array('type'=>'hidden', 'value'=>$password, 'autocomplete'=>'off'));
     'label'=>'Potwierź hasło'));
 echo $this->Form->input('mail', array('label'=>'Adres e-mail'));
 echo $this->Form->input('firstName', array('label'=>'Imię'));
 echo $this->Form->input('lastName', array('label'=>'Nazwisko'));
 echo $this->Form->end('Zapisz');
我的错误是:Error: SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value

在某些情况下(我尝试了很多选项来解决我的问题),它只是给我的flash消息关于注册失败

结果是Password标签不能被禁用

而不是:$this->set('password', $randomString);尝试设置

if ($this->request->is('post') || $this->request->is('put')) {
    $this->request->data['User'] = $randomString;
    if ($this->User->save($this->request->data, true, array('username', 'mail', 'firstName', 'lastName', 'pwd', 'pwd_confirmation'))) {
        $this->Session->setFlash('Rejestracja przebiegła pomyślnie!');
        return $this->redirect(array('controller' => 'pages', 'action' => 'home'));
    }
    $this->Session->setFlash('Rejestracja nie powiodła się, spróbój jeszcze raz.');
    unset($this->request->data['User']['pwd']);
    unset($this->request->data['User']['pwd_confirmation']);
}