【问题标题】:$this->request->data only taking the user's ID when trying to edit CAKEPHP$this->request->data 仅在尝试编辑 CAKEPHP 时获取用户 ID
【发布时间】:2013-02-14 11:48:38
【问题描述】:

我是使用 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 以外的任何其他数据的任何帮助将不胜感激!

谢谢

【问题讨论】:

  • 无需指定'action' =&gt; 'useredit' - 默认情况下它会发布到自己!

标签: cakephp cakephp-2.0


【解决方案1】:
$this->request->data = $this->Session->read('Auth.User.id');

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

你可能是说

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

但即便如此,您仍在从会话中读取“旧数据”。 使用 find(first) 并提供 'Auth.User.id' 仅用于查询正确的记录。 将此记录作为$this-&gt;request-&gt;data 传递到视图。 保存时,确保 id 存在并且它会正确更新。

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

【讨论】:

  • 这已修复数组 $this->request->data,但我的编辑用户更改尚未完成
  • 谢谢马克,之后我意识到我应该在if ($this-&gt;request-&gt;is('post')) {之后调用$this-&gt;request-&gt;data['User']['id'] = $this-&gt;Session-&gt;read('Auth.User.id');
  • Jep :) 不要忘记安全组件和/或白名单,以防止链接中概述的回火。
猜你喜欢
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
相关资源
最近更新 更多