Jump to content

Recommended Posts

Posted

I'm trying to save new values in some existing user fields. I've tried to google this and found lots of information, but unfortunately it seems I'm still going at it wrong. Here's what I've got so far:

Fields are inside a user
$dude = $users->get('dude');

Notes is just a textarea field
$notes = $dude->notes;

This produces a fatal error, why?
$notes->set('He's a dude');

But this works
$notes = 'He's a dude';

Fatal error again, but maybe I should save the user instead
$notes->save();

No error, but nothing gets saved
$dude->save();

I also tried adding in the ->of(false) and ->of(true) before and after saving, but that didn't affect anything.

So in short, how to change user->field values through api and save the results?

Posted
2 hours ago, Hurme said:

This produces a fatal error, why?
$notes->set('He's a dude');

But this works
$notes = 'He's a dude';

Because at this point, $notes is only a string. $dude->notes returns the contents of the field notes, not the field itself.

Why do you need to assign the field to another variable?

In most cases this should be enough.

$dude->notes = "He's a dude";
$dude->set('notes',"He's a dude");
$dude->save();

//or 

$dude->setAndSave('notes',"He's a dude");

 

  • Like 1
Posted

Thanks, that makes sense. For what ever reason I thought it would 'get' the field same way Processwire gets a whole page.

It was originally in one string, but I put it in pieces when I couldn't get it to work. And in the process I broke it even more. Ah well...

Posted
3 hours ago, alxndre said:

$dude->notes = "He's a dude";
$dude->set('notes',"He's a dude");

Keep in mind that these two lines do exactly the same thing, so you only need one of them.

  • Like 2

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
  • Recently Browsing   0 members

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