我的观点:
<tr>
<td>Username</td>
<td><?php echo $this->Form->input('User.username', array('label' => '')); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo $this->Form->password('User.password', array('label' => '', 'value'=>'')); ?></td>
</tr>
我的控制器:
function edit($id = null) {
$this->User->id = $id;
$data = $this->data;
# print_r($data);
if (empty($data)) {
$this->data = $this->User->read();
} else {
if ($this->User->save($data)) {
$this->Session->setFlash('The user details have been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
当我提交带有空白密码的表单时,数据库中的哈希值仍然会发生变化。如果已填写新密码,如何只更新密码哈希值
谢谢。
编辑:$data['User']['password']总是一个哈希值,从不为空!
CakePHP 1.3自动散列password
字段。CakePHP 2.0没有。
你有几个选择(从最差到最好,在我看来):
-
重命名字段并在保存前交换
if ($data['User']['new_password'] != '') { $data['User']['password'] = $this->Auth->password($data['User']['new_password']); }
-
在相等性检查中散列字符串
if ($data['User']['password'] == $this->Auth->password('')) { unset($data['User']['password']); }
-
将哈希函数更改为不散列空密码的哈希函数[参见书中的配置]:
function hashPasswords($data) { if (!empty($this->data['User']['password'])) { $this->data['User']['password'] = $this->Auth->password($this->data['User']['password']); } return $data; }
如果密码字段为空,则取消设置
else {
if(empty($data['User']['password']))
{
unset($data['User']['password']);
}
if ($this->User->save($data)) {
$this->Session->setFlash('The user details have been updated.');
$this->redirect(array('action' => 'index'));
}
}
您可以将$data
中的变量unset
function edit($id = null) {
$this->User->id = $id;
$data = $this->data;
if (empty($data)) {
$this->data = $this->User->read();
} else {
if($data['User']['password'] == ''){
unset($data['User']['password']);
}
if ($this->User->save($data)) {
$this->Session->setFlash('The user details have been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
您可以尝试仅更新特定字段,基于IF大小写:
if(empty($data['User']['password']))
{
$this->User->id = $user_i; // specify which User to update
//update only username
$this->User->save(array('User'=>array('User.username'=>$data['User']['username'])));
}
else{
//save as before
}
你可以使用一个行为来处理它:http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/
你可以使用字段"pwd"并将confirm设置为false(如果你不想使用pwd_confirm字段)