Jump to content

Modifying a repeater field and saving...


celfred
 Share

Recommended Posts

Hello,

I'm back again with my beginner issues... This time, I'd like to update a field found in a repeater across multiple pages.

My code :

 $found = $pages->find("parent=/invitations/$user->name, guests.guest_name=".$form['oldname']);
 foreach ($found as $f) {
	 foreach ($f->guests as $g) {
	 if ($g->guest_name == $form['oldname']) {
		 $f->of(false);
		 $out .= '/01:'.$g->guest_name; // THIS IS TO CHECK IF IT WORKS
		 $g->guest_name = $form['name'];
		 $out .= '/02:'.$g->guest_name; // CHANGE APPARENTLY OCCURRED
		 $f->save(); // BUT NEVER SAVES???
	 }
	 }
 }

Let me try to explain a little : the name of a 'guest' is modified on the front-end. I want to adjust all 'invitations' under the '$user->name' branch (that's how I organized my pages, maybe it's not the best solution...) where the 'oldname' was found (in a 'guests' repeater having 'guest_name' text fields). So I look for the pages and I try to loop each page to check which field has the 'oldname' and when I find it, I change it to the 'newname' ($form['name']) and try to save this change... I have tried many things, but this never worked ! Although I did the same stuff to delete a field (If I remove the guest) and it worked!

I just don't understand, so if someone could give me a hand, I'd greatly appreciate...

Thanks!

  • Like 1
Link to comment
Share on other sites

The problem is that page $f doesn't know the repeater item changed. Those repeater items are individual pages themselves. You can solve this by saving the repeater item ($g) rather than the owning page ($f):

1. Replace your $f->save(); with $g->save();

2. Also replace your $f->of(false); with $g->of(false);

3. Lastly, I also suggest replacing your first line with this:

$found = $pages->find("parent=/invitations/$user->name, guests.guest_name=" . $sanitizer->selectorValue($form['oldname']));

Adding that selectorValue() sanitizer in there will prevent problems from occurring if the guest_name happens to contain characters like commas or quotes.

  • Like 4
Link to comment
Share on other sites

Well THANK YOU very much !

I've made quite a lot of different tries on that problem but wa sunable to solve it by myself and now I go a little step further. That's very nice of you to take the time to provide such detailled explanations in your answers.

I still have a long way to go to better understand those 'pages' issues... But what makes it incredible is how your explanations often make it so simple and clear. That's when I feel like improving a lot in a few seconds and when Processwire becomes something incredibly powerful...

And good thing also about the selectorValue() sanitizer... I've missed that one, although I had problems with commas in another field of my site ;-)

Well... I can then go on with my coding. Thanks again!

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