0

我是使用 CakePHP 的新手,目前我正在尝试允许用户编辑它自己的个人详细信息。

但是尽管该功能似乎有效(它告诉我用户已更新)它实际上并没有更新用户的详细信息。

我在 $this->request->data 上使用了调试器,它似乎显示的只是登录用户的用户 ID。

这是我的功能

public function useredit() {
        $this->User->id = $this->Session->read('Auth.User.id');
        $this->request->data = $this->Session->read('Auth.User.id');
        if ($this->request->is('post') || $this->request->is('put')) {
            //debug($this->request->data);
            //exit;
            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']);
        }
    }

这是我的看法:

<?php
echo $this->Form->create('User', array('action' => 'useredit'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->input('username', array('value' => $this->Session->read('Auth.User.username')));
//echo $this->Form->input('password');
echo $this->Form->input('name', array('value' => $this->Session->read('Auth.User.name')));
echo $this->Form->input('surname', array('value' => $this->Session->read('Auth.User.surname')));
echo $this->Form->input('address1', array('value' => $this->Session->read('Auth.User.address1')));
echo $this->Form->input('address2', array('value' => $this->Session->read('Auth.User.address2')));
echo $this->Form->input('town', array('value' => $this->Session->read('Auth.User.town')));
echo $this->Form->input('postCode', array('value' => $this->Session->read('Auth.User.postCode')));
echo $this->Form->input('dob', array ('value' => $this->Session->read('Auth.User.dob')
                                        , 'label' => 'Date of Birth'
                                        , 'dateFormat' => 'DMY'
                                        , 'empty' => array('DATE','MONTH','YEAR')
                                        , 'minYear' => date('Y') - 110
                                        , 'maxYear' => date('Y') - 0));
echo $this->Form->input('emailAddress', array('value' => $this->Session->read('Auth.User.emailAddress')));
echo $this->Form->input('phoneNumber', array('value' => $this->Session->read('Auth.User.phoneNumber')));
echo $this->Form->input('mobileNumber', array('value' => $this->Session->read('Auth.User.mobileNumber')));
echo $this->Form->end('Submit');
?>

任何帮助找出为什么这不会保存除 id 以外的任何其他数据的任何帮助将不胜感激!

谢谢

4

1 回答 1

2
$this->request->data = $this->Session->read('Auth.User.id');

是错的。也不要在表单视图中设置任何默认值。依赖从控制器传下来的数据。

你可能是说

$this->request->data = $this->Session->read('Auth');

但即便如此,您仍在从会话中读取“旧数据”。使用 find(first) 并提供'Auth.User.id'唯一查询正确记录。然后将此记录传递给视图$this->request->data。保存时,确保 id 存在并且它会正确更新。

请参阅http://www.dereuromark.de/2011/10/05/common-cakephp-problems-and-solutions/了解如何正确执行此操作。请注意,对于 2.x,它的 $this->request->data 而不是 $this->data。也if ($this->request->is('post') || $this->request->is('put')) {}用于 2.x 但基本思想应该清楚了。

于 2013-02-14T11:53:13.497 回答