Jump to content

Direct import of users from a text file?


FuturShoc
 Share

Recommended Posts

Hi, folks!

We're working on our first big website project using ProcessWire. We're no longer in "fiddle and play with PW" mode. We're committed.

My question:

The site will have a directory of organization members, each needing a login/pw and a profile page. We're planning to use the existing Users/Roles model of ProcessWire, but we need to generate several thousand users from a pre-existing text file.

Now, I won't have a problem writing a plain PHP script to parse the text file and interacting with the database. BUT i could use some help understanding how the user details and fields are laid out in PW's default install. I'm discovering, as many of you already know well, that even users are just pages. This, however, makes it a bit less than obvious how one might generate users programmatically on the fly.

I would welcome any input or guidance you might have in helping me know *where* to insert *what* into the DB schema.

I'd also welcome any anecdotal experiences you may have had doing something like this yourself. Caveats?

Link to comment
Share on other sites

You are right. If you need more fields (first name, last name, image etc.), you can add them to the user template.

$u = new User();
$u->firstname = "Gonzo";
$u->lastname = "Ipsum";
$u->name = "gonzo";
$u->pass = "BamBam!";
$u->addRole("guest");
$u->save(); 
  • Like 1
Link to comment
Share on other sites

Next to the using the API for this task you could also use this module for the task. I haven't tried it with Users myself but seeing that they are also 'just' pages i think it should be easy to import a couple of thousand users from a csv file. The module also gives some options on updating via csv.

Link to comment
Share on other sites

You can also do this:

$u = $users->add('gonzo'); 
if($u->id) {
  $u->firstname = 'Gonzo';
  $u->lastname = 'Ipsum';
  $u->pass = 'BamBam!';
  $u->addRole('guest');
  $u->save();
} else {
  echo "That username is already taken, please choose another";
}

The benefit of using $users->add() is that it actually creates and saves the user before you populate it, meaning you can exit out earlier if there is some error. But the main reason is that some fields (like files or images) require the page you are manipulating to have an ID before you can add files. So this saves you from having to think about that. If that's not a consideration, then it doesn't really matter which approach you take. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...