【发布时间】:2015-06-18 22:28:46
【问题描述】:
所以我一直在尝试让编辑用户功能在我的应用程序中运行,但我对如何使用 CakePHP 3 执行此操作有点困惑。这就是我的编辑操作在我的 UsersController.php 中:
public function edit() {
$this->layout = 'dashboard';
$user = $this->Users->get($this->Auth->user('id'));
if ($this->request->is(['post', 'put'])) {
$this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('Your account has been edited'));
return $this->redirect(['controller' => 'Users', 'action' => 'edit']);
}
$this->Flash->error(__('Your account could not be edited. Please fix errors below.'));
}
$this->set(compact('user'));
}
在我的 edit.ctp 文件中:
<?php
$this->Form->templates($form_templates['defaultBootstrap']);
echo $this->Form->create($user);
?>
<fieldset>
<legend><?php echo __('Edit Profile'); ?></legend>
<?php
echo $this->Form->input('email', [
'label' => __('Email'),
'placeholder' => __('Email'),
'autofocus'
]);
echo $this->Form->input('currency', [
'label' => __('Default Currency'),
'options' => [
'CAD' => 'CAD',
'USD' => 'USD'
]
]);
echo $this->Form->input('password', array(
'label' => __('Password'),
'placeholder' => __('Password'),
'value' => ''
));
echo $this->Form->input('confirm_password', array(
'label' => __('Confirm Password'),
'placeholder' => __('Confirm Password'),
'type' => 'password'
));
?>
</fieldset>
<?php
echo $this->Form->submit(__('Edit'));
echo $this->Form->end();
?>
这个问题是附加到表单的密码是散列的,所以当我使用patchEntity时,它再次被散列,因为在实体User.php中:
protected function _setPassword($password) {
return (new DefaultPasswordHasher)->hash($password);
}
当我在控制器中设置 $user 时,我也尝试过不获取密码。但是当我使用 patchEntity 时,它只是对空白值进行哈希处理。
也许我的做法完全错误,我只是在寻找有关如何解决这个问题的方向,如果有人可以帮忙的话。
【问题讨论】:
标签: cakephp cakephp-3.0