Jump to content

How to create a new user in api with multiple template or parent


adrianmak
 Share

Recommended Posts

Follw up with my previous post

https://processwire.com/talk/topic/12782-regarding-multiple-templates-or-parents-for-users/

For default installation, api to create a new user is something like that

$u = new User();
$u->of(false);
$u->name = "adrian";
$u->email = "adrian@example.com";
$u->pass = "123456";
$u->addRole("registered");
$u->save();
$u->of(true);

With multiple template or parent , how to setup a user object to use which template and parent, before executing save() method ?

  • Like 4
Link to comment
Share on other sites

(Untested) It should be sufficient to assign the correct value for parent (the User class inherits from Page) and pass the correct template in the constructor.

$u = new User($templates->get('my-user-parent')); // pass the Template object
$u->parent = 222; // you can also pass a Page object or selector string here
// continue as usual...
  • Like 2
Link to comment
Share on other sites

(Untested) It should be sufficient to assign the correct value for parent (the User class inherits from Page) and pass the correct template in the constructor.

$u = new User($templates->get('my-user-parent')); // pass the Template object
$u->parent = 222; // you can also pass a Page object or selector string here
// continue as usual...

Before I got your suggested answer,

I tested with

$u->template = "my-user-parent";

and it work.

I also tested with yours. it work too.

Link to comment
Share on other sites

  • 3 years later...

Im  trying to create an option to Add new users from the front end.

$u = $users->add('gonzo');
$u->pass = "BamBam!";
$u->addRole("superuser");
$u->save();

But there is a problem because the users is somehow created, and I can access all data using the API, but it's not displayed under /admin/access/users/> If I try to access the user Id from the processwire api adding to the url admin/access/users/edit/?id=92871 it says that the page is not existing. But If I call the ID by $users->get(92871) I have the full data.

Any suggestions? 

Link to comment
Share on other sites

On 2/3/2020 at 2:50 PM, theqbap said:

the url admin/access/users/edit/?id=92871 it says that the page is not existing. But If I call the ID by $users->get(92871) I have the full data

That's strange. The code works fine here (3.0.132). Can you take a look into the pages table and compare the entry for id 92871 with a working user (parent_id, templates_id, status)?

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...