Hurme Posted September 13, 2019 Share Posted September 13, 2019 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? Link to comment Share on other sites More sharing options...
alxndre Posted September 13, 2019 Share Posted September 13, 2019 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"); 1 Link to comment Share on other sites More sharing options...
Hurme Posted September 13, 2019 Author Share Posted September 13, 2019 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... Link to comment Share on other sites More sharing options...
adrian Posted September 13, 2019 Share Posted September 13, 2019 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. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now