$ this-> request->数据仅在尝试编辑CAKEPHP时获取用户ID

时间:2013-02-14 11:48:38

标签: cakephp cakephp-2.0

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

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

我在$ this-> request->数据上使用了调试器,它似乎显示的是用户登录的用户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之外的任何其他数据将不胜感激!

由于

1 个答案:

答案 0 :(得分: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-&gt; request-&gt;数据而不是$ this-&gt;数据。 同时使用if ($this->request->is('post') || $this->request->is('put')) {}表示2.x. 但基本的想法应该变得清晰。