views:

117

answers:

2

Hello,

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

A: 

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));
Darren Newton
I made those modification but instead of $this->Auth->user('id') I used $this->Auth->user(), because I need to load all the informations about the User. But I still get blank page and no modifications on Users. The output of print_r($this->User); is: Array ( [User] => Array ( [id] => d12b1341-ee3e-11de-90ad-368a14c18e81 [login] => NoOne [email_address] => [email protected] [group_id] => 4b2f7282-cc98-4db0-8994-0cd49279ab65 [nr] => 2 [active] => 1 [created] => 2009-12-20 01:18:55 [modified] => 2009-12-21 13:09:13 ) )
NoOne
+2  A: 

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));
}
mscdex
That's it thank you ;)
NoOne
You should use `$user = ClassRegistry::init('User');` instead.
deizel