Of course, doing a direct operation on the database bypasses the entire Drupal API, so other modules don't get a chance to interact. It assumes full control of the database, and, while often useful, has that downside. Operations like this bypass the capabilities of Role Change Notify and other modules that might be listening for user changes.<br>
<br>I generally recommend using the API unless there's a huge performance reason not to.<br><br>-Randy<br><br><div class="gmail_quote">On Fri, Feb 26, 2010 at 5:29 PM, Steve Edwards <span dir="ltr"><<a href="mailto:killshot91@gmail.com">killshot91@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div style="word-wrap: break-word;">Yup, I like the more direct route like that. Works great.<div>
<br></div><div>Thanks.</div><div><br></div><div>Steve</div><div><br><div><div>On Feb 26, 2010, at 3:40 PM, nan wich wrote:</div><br><blockquote type="cite"><span style="border-collapse: separate; font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div>
<div style="margin: 0px; font-family: arial,helvetica,sans-serif; font-size: 12pt;"><div style="margin: 0px;"></div><div style="margin: 0px;">You don't need to load the whole user object to do this - way too much potential overhead.</div>
<div style="margin: 0px;"> </div><div style="margin: 0px;"><font face="Courier New, courier, monaco, monospace, sans-serif"> $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $name_you_just_got));</font></div>
<div style="margin: 0px;"><font face="Courier New, courier, monaco, monospace, sans-serif"> db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid_of_role_to_add);<br></font> </div><p><font size="4" face="bookman old style, new york, times, serif" color="#ff007f"><i><b>Nancy E. Wichmann, PMP</b></i></font></p>
<p><font face="arial, helvetica, sans-serif">Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.</font></p><div style="margin: 0px;"><br></div><div style="margin: 0px; font-size: 12pt; font-family: arial,helvetica,sans-serif;">
<br><div style="margin: 0px; font-size: 10pt; font-family: arial,helvetica,sans-serif;"><font size="2" face="Tahoma"><hr size="1"><b><span style="font-weight: bold;">From:</span></b><span> </span>Steve Edwards <<a href="mailto:killshot91@gmail.com" target="_blank">killshot91@gmail.com</a>><br>
<b><span style="font-weight: bold;">To:</span></b><span> </span>Drupal Development <<a href="mailto:development@drupal.org" target="_blank">development@drupal.org</a>><br><b><span style="font-weight: bold;">Sent:</span></b><span> </span>Fri, February 26, 2010 4:49:02 PM<br>
<b><span style="font-weight: bold;">Subject:</span></b><span> </span>[development] How to add user role during registration with custom form<br></font><br>I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing.<br>
<br>Is there a better (and easier) way to do this that I'm missing, or am I on the right track?<br><br>Thanks.<br><br>Steve</div></div></div></div></span></blockquote></div><br></div></div></blockquote></div><br><br clear="all">
<br>-- <br>Randy Fay<br>Drupal Development, troubleshooting, and debugging<br><a href="mailto:randy@randyfay.com">randy@randyfay.com</a><br>+1 970.462.7450<br><br>