你为什么在这里查询会话?当然这会在保存之后再次获得旧数据。
使用数据库一如既往,再次更新数据库,然后才可能覆盖会话(你似乎是用饼1.3):
public function profile() {
$uid = $this->Session->read('Auth.User.id');
if (!empty($this->data)) {
$this->data['User']['id'] = $uid;
if ($this->User->save($this->data, true, array('email', 'first_name', 'last_name', 'id', ...))) {
// if you rely on auth session data from the user, make sure to update that here
$this->Session->write('Auth.User.email', $this->data['User']['email']); // etc
...
// OK, redirect
} else {
// ERROR
}
} else {
$this->data = $this->User->find('first', ...);
}
}
正如你可以看到我更新过的会话密钥改变。
如果使用2.x的(你没有指定为现在),你也可以使用
$this->Auth->login($this->request->data['User']); // must be the User array directly
虽然你将不得不小心地通过所有已在会议之前的数据。 如果您计划使用login(),最好再次找到(第一个)更新的记录,然后将其传递给login()。
但个人而言,我更喜欢只更新实际更改的字段。
请参阅Editing own account/profile
什么cakephp版本?您需要始终提供此信息以获取正确的答案。 – mark 2013-03-07 09:14:36
@amit:Cake提供了非常方便的debug()方法,可以使用调试级别0(在生产中)和调试级别1或2(用于开发)。你可以用debug($ this-> data)替换你的print_r; – 2013-03-07 14:19:16