2

添加用户控制器.php

public function add() {
    if ($this->request->is('POST')) {
        if ($this->confirmPass()) {
            //Want false
            if (!$this->validateSQL('username', 'add')) {
                $this->Session->write('User.username', strtolower('User.username'));
                //Want false
                if (!$this->validateSQL('email', 'add')) {
                    $this->Session->write('User.email', strtolower('User.email'));
                    $this->User->create();
                    if ($this->User->save($this->request->data)) {
                        $this->Session->setFlash(__('Your account has been created'), 'flash_success', array('class' => 'success'), 'success');
                        $this->redirect(ACCOUNT_LOG);
                    } else {
                        $this->Session->setFlash(__('Your account could not be created'), 'flash_failure', array('class' => 'failure'), 'failure');
                    }
                } else {
                    $this->Session->setFlash(__('E-mail already in use'), 'flash_failure', array('class' => 'failure'), 'failure');
                }
            } else {
                $this->Session->setFlash(__('Username already in use'), 'flash_failure', array('class' => 'failure'), 'failure');
            }
        }
    }
}

validateSQL 只调用检查用户名和/或电子邮件字段以查看它们是否存在(我将它们留在控制器中,因为我想先解决这个问题,我知道我需要将它们移动到模型中) .

confirmPass 仅比较视图中的字段密码和确认密码,返回真或假。

之前保存在 User.php

public function beforeSave($options = array()) {
        if (isset($this->request->controller->data[$this->alias]['password'])) {
            $this->request->controller->data[$this->alias]['password'] = AuthComponent::password($this->request->controller->data[$this->alias]['password']);
        }
        return true;
}

昨天一切正常。所有值仍然保存到正确的表中,只是没有对密码进行哈希处理(md5+salt)。如果这是 AuthComponent 的已知或常见问题,是否有人有任何想法?

如果我删除我从模型中得到的 isset:

注意(8):试图获取非对象的属性 [APP/Plugin/Account/Model/User.php,第 65 行]

注意(8):试图获取非对象的属性 [APP/Plugin/Account/Model/User.php,第 65 行]

注意(8):间接修改重载属性User::$request无效[APP/Plugin/Account/Model/User.php,第65行]

4

1 回答 1

0

改用这个:

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}

原因: 在模型中你只需要$this->data,而不是$this->request->controller->data

于 2013-05-17T10:55:05.387 回答