tags:

views:

169

answers:

1

I have a module that implements hook_init.

Within that I have a case where I try to call user_save like save

user_save ('');

I figure this should work since I am not setting a uid, so it should create a new one. It also says that the $array should be allowed to be empty. However, this always returns FALSE. ??

I have also tried setting these values in $array but this doesn't work either:

     $foo = array(
   'name' => 'the new user',
   'mail' => 'the new user mail',
   'pass' => user_password(),
   'status' => 1,
   );
     $new_user = user_save ('', $foo);
+1  A: 

Look at the documentation:

Parameters
$account The $user object for the user to modify or add. If $user->uid is omitted, a new user will be added.
$array (optional) An array of fields and values to save. For example, array('name' => 'My name'); Setting a field to NULL deletes it from the data column.

So when you call user_save() the first parameter must be a user object. If that user object doesn't have a uid, or is a string like in your case, a new user will be created. Here the 2nd parameter is necessary. It's not required to use the function, but as it holds all the values you want to save, you wont get far trying to create a user with no data at all. What use is a user without password, username etc. So a minimum of data is required as well, so Drupal will know what to save.

googletorp
Hortitude

related questions