I have a controller named sales_controller at this controller I got a function in wich I want to:
- update information about the sale -> DONE
- create a new record on other model -> DONE
- 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
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));
}
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));
精彩评论