Jump to content

Problem setting created_users_id


adrian
 Share

Recommended Posts

Ok, having a weird issue. I am including ProcessWire from another PHP script. From this script I am saving a new PW page. Everything works except that the page is saved with created_users_id and modified_users_id as guest, rather than the logged in user. I have tried manually adding changing these fields with several variations including these, but they still contain the guest id:

$page->set('created_users_id', $user_id);
$page->created_users_id = $user_id;
 

This works fine:

$user_id = (int)$_GET['user_id'];
$user = wire('users')->get($user_id);
print $user->name
 

so I know that $user_id is correct.

Should I have to manually set the created and modified user id's in this situation?

Even if I shouldn't have to, I still don't understand why I can't!

Thanks for any suggestions.

Link to comment
Share on other sites

I'm not sure, but it might be worth checking if a new page created in the admin actually adds the user ID to the modified_users_id when creating a page that way as theoretically the page hasn't yet been modified.

It's a long shot and I'm fairly sure I'm wrong, but it would be worth checking that first if only to rule it out.

Link to comment
Share on other sites

Hey guys - created_users_id and modified_users_id are both set to 40 (Guest ID). The API calls add the page and populate various fields, save it, then add an image and save again. I am guessing that is why modified_users_id is set, or maybe it actually gets set anyway on new page creation - I haven't checked.

Maybe I should better explain what is happening. I have a full PW site where a front-end user logs in. The PW site redirects to a URL on another domain (but same server) which includes PW via include("/path/to/pw/index.php"); This external site creates the PW page.

I just tried this from the external site to be sure of user logged in status:

if($user->isLoggedin()){
    print 'uid: ' . $user->id;
}
 

and it correctly returns the logged in user's id.

Got me baffled. It seems like it should be populating these fields automatically, but even if it doesn't, I can't figure out why I can't do it manually.

Thanks again.

Link to comment
Share on other sites

Just to follow up - I couldn't figure how to solve this via the API - every time I saved the page it kept setting the user's ID to 40, so I ended up resorting to an SQL UPDATE SET after the last save to override things, which is working fine.

Link to comment
Share on other sites

Looks like theres no user logged in in your case because it saves the user logged in with the request. Works fine here. My guess would be PW version or because you are having different domain where you lose login or session or alike.

Link to comment
Share on other sites

Still not sure what you want to do or what is wrong.

Just to verify.

Page modified and created, so modified_users_id and created_users_id get set on page save. You can't overwrite it, since it done on every save.

When creating page it set's both, which is desired.

When creating page from bootstrap the current user is used to save id according.

If you want to overwrite some of it, you'd have to make it through SQL manually.

See some post here http://processwire.com/talk/topic/651-set-created-by-creating-a-page-via-api/

Link to comment
Share on other sites

Hey Soma,

Thanks for the suggestions. As I mentioned above, I have already taken care of things with SQL - does exactly what I need it to do. I just don't understand why the $page-save() isn't taking care of it. I initially assumed it was losing the logged in user too, but as I said, when I do the following just before the $page->save():

if($user->isLoggedin()){
    print 'uid: ' . $user->id;
}
 

it prints the correct id for the user that is logged in.

I think if no-one else is having this problem, then we shouldn't worry about it for now.

Link to comment
Share on other sites

As I said before, it works the way it should. It does get saved with the user id (user logged in or if not as 'guest'). It's there in the Pages::save and does work.

If it doesn't work in your case, as I said previous it may has something to do with:

.... The PW site redirects to a URL on another domain (but same server) which includes PW via include("/path/to/pw/index.php"); This external site creates the PW page.

Session's usually don't transfer to another domain, even if on same server.

Link to comment
Share on other sites

Hey Soma,

Thanks - I understand that it is working in a normal use with both pages on the same domain.

And I realize that different domains are the issue, and I am happy with the manual SQL fix. What I can't figure out is why the isLoggedin() check shows that the correct user is logged in. It doesn't really matter in the end, but it does seem weird.

Link to comment
Share on other sites

Yep, this is indeed strange, and I have no idea why. Maybe if you could share some light on the exact setup you have. Where are the script and where included/called...

Maybe someone has an idea then. :)

Link to comment
Share on other sites

May I ask how I can include the "created_by" field on a page, since in the settings Tab it won't show up. Is this field included in the basic installation or do I have to create it by myself and if yes how?

Link to comment
Share on other sites

  • 2 weeks later...

Thanks, that was easy!

Now I would like to get the name, email or any other custom field associated to a user:
What I tried, doesn't work:

$page->created_users_id->name; 

$page->created_users_id->email;

How is the syntax for reaching those fields?
 

Link to comment
Share on other sites

Ok, bit of a hiatus on this issue, but I think I finally know what was going on - maybe!

Because I was having to manually redefine $user (session not transferring), things were not properly in place for automatic saving of created_users_id when the page was saved - it was saving as the guest user.

I haven't tested this yet, but I just stumbled across this setting "Allow the 'created user' to be changed on pages?" which apparently needs to be checked to ".... enable the $page->createdUser or $page->created_users_id fields to be saved via the API". I think that would then allow this to work: $page->created_users_id = $user_id; 

Since that project is now finished and the manual SQL approach worked, I probably won't bother to check, but thought it might help someone else. 

BTW, that setting is only available with $config->advanced set to true.

Link to comment
Share on other sites

I haven't tested this yet, but I just stumbled across this setting "Allow the 'created user' to be changed on pages?" which apparently needs to be checked to ".... enable the $page->createdUser or $page->created_users_id fields to be saved via the API". I think that would then allow this to work: $page->created_users_id = $user_id; 

That's correct. But it also allows it to be changed in the page editor. Here is the description field that goes along with it: 

"When checked, pages using this template will have an option to change the 'created by user' (for superusers only). It will also enable the $page->createdUser or $page->created_users_id fields to be saved via the API."

Note that in order to access this setting, $config->advanced=true; has to be set in /site/config.php

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...