Cakephp - 更新已登录用户的信息

发布于 2024-08-15 18:53:10 字数 628 浏览 2 评论 0原文

我有一个名为 sales_controller 的控制器,在这个控制器上我有一个函数,我想:

  1. 更新有关销售的信息 ->完成
  2. 在其他模型上创建新记录 ->完成
  3. 更新用户记录上的字段 ->问题

我最后一次尝试这样做是:

App::import('model','User');
$user= $this->Auth->user();
$nr = $this->Auth->user('nr') - 1 ;

if($user->save(array('nr'=>$nr)))
{
    $this->Session->setFlash(__('DONE! ', true));
    this->redirect(array('action'=>$page,$params));
}

我知道方法 $this->Auth->user() 返回一个数组,并且我读到“保存”对数组不起作用...

我试图调用 read对用户起作用,但我仍然不知道应该怎么做。

这一定是很简单的事情,但我还是个新手哈哈。

那么,有人可以帮助我吗?

谢谢

I have a controller named sales_controller at this controller I got a function in wich I want to:

  1. update information about the sale -> DONE
  2. create a new record on other model -> DONE
  3. update a field on a user record -> PROBLEM

My last attempted to do this was:

App::import('model','User');
$user= $this->Auth->user();
$nr = $this->Auth->user('nr') - 1 ;

if($user->save(array('nr'=>$nr)))
{
    $this->Session->setFlash(__('DONE! ', true));
    this->redirect(array('action'=>$page,$params));
}

I know that the method $this->Auth->user() returns an array and I read that "save" does not work for arrays...

I was trying to call read function on users, but I still don't know how I should do it.

This must be something simple to do, but I'm still a newbie lol.

So, can anyone help me?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-08-22 18:53:10

试试这个:

App::import('Model', 'User');
$user = new User();
$user->id = $this->Auth->user('id');

/* if you really need to store the entire array, instead of accessing individual fields as needed */
$user_info = $this->Auth->user();

/* in saveField, add 'true' as a third argument to force validation, i.e. $user->saveField('nr', $this->Auth->user('nr') - 1, true) */
if ($user->saveField('nr', $this->Auth->user('nr') - 1)) {
   $this->Session->setFlash(__('DONE! ', true));
   this->redirect(array('action' => $page, $params));
}

Try this:

App::import('Model', 'User');
$user = new User();
$user->id = $this->Auth->user('id');

/* if you really need to store the entire array, instead of accessing individual fields as needed */
$user_info = $this->Auth->user();

/* in saveField, add 'true' as a third argument to force validation, i.e. $user->saveField('nr', $this->Auth->user('nr') - 1, true) */
if ($user->saveField('nr', $this->Auth->user('nr') - 1)) {
   $this->Session->setFlash(__('DONE! ', true));
   this->redirect(array('action' => $page, $params));
}
雅心素梦 2024-08-22 18:53:10

您已经加载了 User 对象,但没有对它执行任何操作。您已将 $user 的值设置为从 Auth 对象返回的数组,然后尝试对其调用 save 方法。您想要对 User 对象调用 save。

我想你想说的是:

$this->User->id = $this->Auth->user('id');
$this->User->save(array('nr'=>$nr));

You've loaded the User object but you're not doing anything with it. You've set the value of $user to an array returned from the Auth object, and then you're trying to call the save method on it. You want to call save on the User object.

I think what you're trying to say is:

$this->User->id = $this->Auth->user('id');
$this->User->save(array('nr'=>$nr));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文