我目前正在使用 CAKEPHP 2.3,并且正在尝试编辑用户信息。但是当我提交信息时,它不会将信息更新到数据库中。相反,它会使用我插入的新信息创建一个新用户。我希望它更新用户,而不是创建一个新用户。
我的 edit.ctp 代码是:
<h1>Edit Account information</h1>
<?php
echo $this->Form->create('User', array('action' => 'edit'));
echo $this->Form->input('username', array('value' => $this->Session->read('Auth.User.username')));
echo $this->Form->input('name', array('value' => $this->Session->read('Auth.User.name')));
echo $this->Form->end('Submit');
?>
然后我在用户控制器中的编辑功能是:
public function edit() {
// debug($this->User->save($this->request->data));
$this->User->id = $this->Session->read('Auth.User.id');
$this->request->data['User']['id'];
if ($this->request->is('post')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The User has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
}
} else {
$this->request->data = $this->User->read(null);
isset($this->request->data['User']['password']);
}
}
这是进入编辑用户页面的索引。
<h1>Users Home</h1>
<p>Welcome <?php print $this->Session->read('Auth.User.name');?> <br/></p>
<table border="0" width="200" text-align="center">
<tr>
<td width="50"><?php echo $this->Html->link('Log out', array('action' => 'logout')); ?></td>
<td width="50"><?php echo $this->Html->link('Edit', array('action' => 'edit')); ?></td>
<td width="50"><?php echo $this->Html->link('Add User', array('action' => 'add')); ?></td><!-- should only show for admin -->
<td width="50"><?php echo $this->html->link('Manage Users', array('action' => 'usermanage')); ?></td><!-- should only show for admin -->
</tr>
</table>
非常感谢。