0

With each user I create when my form is submitted, I want to insert an id into a field/column I've added to the users table. Trouble is, the column isn't receiving the value. I've tried a few variations of user_save() to coax it into inserting the value, but at this point I think I'm missing something fundamental of drupal 7. Later I also need to retrieve this value from the current user.

I'd appreciate any help. Thanks.

<?php
//Func called in form submit. Values passed from there
function fsa_create_sponsor_admin($sponsor_id, $a_fname, $a_lname, $a_email){

//Misc. code. . . .

//create a new user
$account = new stdClass();
$account->is_new = TRUE;
$account->name = $user_name;
$account->mail = $a_email;
$account->init = $a_email;
//$account->sponsor_id = $sponsor_id; (no error, but not working here)
$account->status = TRUE;
$account->roles = $new_user_roles;
$account->timezone = variable_get('date_default_timezone', '');

//First variation of user_save()
user_save($account, array('pass' => $sp_password, 'sponsor_id' => $sponsor_id));

//Second variation of user_save()
user_save($account, array('pass' => $sp_password, 'field_other' => array(LANGUAGE_NONE => array(array('sponsor_id' => $sponsor_id)))/*end 2nd array*/));
}
?>

Column added in hook_install()

db_add_field('users', 'sponsor_id', array('type' => 'int', 'size' => 'big', 'not null' => FALSE, 'description' => 'Sponsor to which user belongs',));
  • 1
    I have it on good authority (from one of the core maintainers) that it's not a good idea to add extra fields to the core tables. That said, if you want to do it you also need to implement hook_schema_alter() to inform the system about the new column and hook_user_presave() to set it on the $user object. See this post for an example that does the same thing for the node table – Clive Jun 30 '12 at 14:43
  • @Clive -- I'm curious, why is it not a good idea? the user table has alot of attributes already, why is it "bad" to add more. if you prefix the column names with say your app name (i did modify users once for a project i have now, i do like dew_surveys_all_completed) ... there will never be a name clash ever .... i get its not Kosher. But it makes SQL easier for one ... – tenken Jun 30 '12 at 14:48
  • @tenken Sorry, poor choice of words, it's not that it's not a good idea as such; I was told it was a 'Drupal 5 way of thinking', which is a fair point since the field system is the preferred way of adding extra data to entities these days. I do agree with you though, I personally don't see the harm (it was me that answered the question on how to do this that I linked to), I just thought I'd pass on the wisdom from the top so-to-speak – Clive Jun 30 '12 at 14:55
  • 1
    Wouldn't the approach of adding extra fields to core tables make them inaccessible to Views (for one), whereas using the field API automatically exposes them to basically every module everywhere. – David Meister Jun 30 '12 at 16:35
  • Thanks Clive! After implementing hook_schema_alter & hook_user_presave I was able to add a value to my field as I would for the name or mail "$account->sponsorid." Your reference to the other post was also very helpful. David, thanks! I didn't know this approach would cause an issue with Views. Although I don't plan on making a very complex module, I want to to be as flexible as possible. I'll likely revisit this code using the field API later. – SFox Jun 30 '12 at 17:52
1

I had the a similar problem. This code helped me. I put this code in myform_submit.

$fieldvalue = $form_state['values']['fieldname'];

//Loading the user object

$account = user_load($GLOBALS['user']->uid);

//Gettin the user data

$edit['data']['fieldname'] = $fieldvalue;

//saving the achieved data

user_save($account, $edit);

//Retrieving the user value

global $user;
$whatever = $user->data['fieldname']; 
  • Thanks for your help Sandesh! Initially I tried Clive's suggestion and got it to work, so I didn't get a chance to test & use your code. Regardless, I sincerely appreciate it. – SFox Jun 30 '12 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.