Hi,

We are an association that has members, and the number of members is always going up and down. I need to write an external to Drupal php script that takes an export of our users and updates the User table and the coresponding profile fields.

Now I started doing this and from a programming point it's not a problem. Then I noticed this field data in the user table that seems to contain all the data in the users profile. The format is :
s:15:"a_paddress_2";s:13:"P.O. Box 4212";s:11:"aspe_pstate";

If I update or change the user / profile values table outside of Drupal will it screw anything up if I don't update this data field? Is there any documentation on this field and it's use?

I am just learning about writing modules so I don't have time to learn this before as I need to get this in place first.

Thanks,

Richard S Albrecht

Comments

briansea’s picture

Yes and No. I have done it with success; however, if the profile fields get restructered via the drupal admin panel, your code breaks. So, it is advisable to use drupal functions to do it. To clarify, are you just speaking of the data colomn? or also of the profile_fields and profile_vlaues tables? if just the data colom, here is how to interact with it: it is just a serialized assoc. array... SO, you can work with it like that, or, you can use the drupal functions:

to load a user, from an external script lets say

include_once 'includes/bootstrap.inc'; 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 

$user = user_load(array('uid' => $someUid));
$extra_data = array('Mood' => t('happy')); 
user_save($user, $extra_data); 

//then the user obj. has a new property
print $user->mood; 

and, its stored in the db

Hope this helped!

Hawkcode’s picture

THanks for the code, learning Drupal programming is going to be a journey.

Here's what I need to do:

1. Export the Membership Data to a csv, this is all stored in Access, Yea I know I'm trying to get them to move it.
2. Take each user from the csv. 1 at a time and pull that record from drupal, User table and all the profile fields. Compare them and if the csv value is different for that field, then replace the value in the profile table or user table.

I will definitely will use the Drupal Function.

Rich

Hawkcode’s picture

I got another question you may know the answer to.

There are 2 date fields in the profile. They don't show up when you go to the My Account page. Then I created a quick view and the date fields were showing as 00/00 then in fact looking at the table showed a valid date?

There are also a few other of the 35 profile fields that are not showing up.

Thanks again!

briansea’s picture

how/where did u add the fields?

Hawkcode’s picture

The fields were all added through the Drupal Interface.

Hawkcode’s picture

Tried running the code snipet and got the folowing error:

Compile Error: ./includes/bootstrap.inc line 1003 - require_once() [function.require]: Failed opening required './includes/cache.inc' (include_path='.:/Applications/MAMP/bin/php5/lib/php')

I was stepping through the code and tried running it byitself and from a link in a Drupal Node. Same thing both times.

Any Ideas?

Rich

briansea’s picture

are you running drupal 6? Additionally, the code snip should be run from the root of your drupal install

Hawkcode’s picture

Running 6 and I did try running it from one directory under it.

I moved it to the root and tried running it my itself on got the message:

Site off-line

The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.


I was able to run it by launching it from within drupal.

I don't understand why I couldn't run it one level deep and making the path: "./includes/bootstrap.inc"

This is going to clutter up my root till I can get up to speed writing modules.

Thanks

Hawkcode’s picture

Is there a Drupal fuction to search for a record by a value in the profile?

Thanks for all your help.

Hawkcode’s picture

Got this all working, but there are 2 things I can't get right.

1. Can't seem to put a date in a profile date field.

2. How do I add roles to the user_save()

briansea’s picture

2. user_save($account, $array)

the 2nd param is an array of data to store for the user. It can have a key ofroles like:


$roles = array(
  1 => 'Role1',   //where 1 is a role id (rid) you wanna add
  2 => 'Role2',   //where 1 is a role id (rid) you wanna add
);

$array = array(
  'roles' => $roles,
);
//then call user_save($account, $array);

//NOTE: this will delete all existing roles for that user

//to avoid this:
//get the roles on the user by

$existingRoles = $user->roles; // this is an array like: array(2 => 'authenticated user', BLAH) 

//then merge it with your new roles like this:

$newRoles = array_merge((array)$roles, (array)$existingRoles);

//then call user save

briansea’s picture

The date is stored as a seralized array. Jan 2nd 2003 then looks like: a:3:{s:5:"month";s:1:"1";s:3:"day";s:1:"2";s:4:"year";s:4:"2003";} when serialized.

Or $data = array('year' => '2003', 'month' => '1', 'day' => '2');

then call serialize($date); and store the result.

Hawkcode’s picture

Thank you so much. I now have a complete import routine.

I was originally planning to have this routine also check to se if there are any changes and then if so do a user save. But I'm now wondering if that will be an faster than just saving the record and moving on as I would have to compare each field and if a change was detected then do the save.

Any opinion on this?

Thanks again. Next step turn this into a true module.

Rich

briansea’s picture

I'd just save it and move on. :)

I'm very glad to have helped.

Brian