renobird Posted May 21, 2013 Share Posted May 21, 2013 Is there a way other than the removeAll() method to update existing repeater fields? I have pages that have 10 repeaters—each page may get edited a dozen times or more. What happens with removeAll() is each time the page is saved, the repeaters are removed and recreated — assigning them a new page id. The page id numbers start to grow like crazy. For example: 1 page with 10 repeaters that gets edited 10 times = 100 new page ids. Not a huge deal. Except, there could be 20-30 pages that get that many edits. Now we are talking about 2000-3000 new page ids — every day. So I'm looking for another method to update the existing repeaters.There will always be the same 10. No more, no less. A gentle nudge in the right direction would be appreciated. Link to comment Share on other sites More sharing options...
renobird Posted May 21, 2013 Author Share Posted May 21, 2013 Can repeater pages be edited directly? Seems like even if I know the ID of the page, I can't update it via the API. Link to comment Share on other sites More sharing options...
Wanze Posted May 21, 2013 Share Posted May 21, 2013 Hi renobird, I did a quick test and it worked for me. Just updated the first page of a repeater field with the API: $firstPage = $pages->get('/myRepeaterPage/')->repeater_field->first(); $firstPage->of(false); $firstPage->field = 'new value'; $firstPage->save(); Can you post some example code? Cheerio Link to comment Share on other sites More sharing options...
renobird Posted May 21, 2013 Author Share Posted May 21, 2013 Hi Wanze, I'm not sure what is going on actually, I was trying something very similar. $rp = $pages->get('id=4591'); $rp->of(false); $rp->form_expense_description = 'test'; $rp->save(); that gives me these errors: Notice: Trying to get property of non-object in /xxxx/wire/core/PageAccess.php on line 25Notice: Trying to get property of non-object in /xxxx/wire/core/PageAccess.php on line 27Fatal error: Call to a member function hasRole() on a non-object in /xxxx/wire/core/Page.php on line 1275 This error message was shown because site is in debug mode ($config->debug = true; in /site/config.php). Error has been logged. Link to comment Share on other sites More sharing options...
renobird Posted May 21, 2013 Author Share Posted May 21, 2013 echo $rp->title; that gives me the correct page title, so the get seems to work. Link to comment Share on other sites More sharing options...
renobird Posted May 21, 2013 Author Share Posted May 21, 2013 Hang on. I think I understand now. I was going about that wrong. 1 Link to comment Share on other sites More sharing options...
renobird Posted May 21, 2013 Author Share Posted May 21, 2013 Thanks Wanze, I managed to get it sorted out. $n = 1; $type = "expense$n"; $desc = "desc$n"; $amount = "amount$n"; $pm = "method$n"; $expenses = $p->travel_expense; // the repeaters foreach ($expenses as $expense){ $rp = $pages->get("id=$expense->id"); // the repeater page we want to update $rp->of(false); // update repeater fields $rp->expense_type = $input->post->$type; $rp->expense_description = $sanitizer->text($input->post->$desc); $rp->expense_amount = filter_var($input->post->$amount,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); $rp->payment_method = $input->post->$pm; // save the repeater page $rp->save(); // update counter etc $n++; $type = "expense$n"; $desc = "desc$n"; $amount = "amount$n"; $pm = "method$n"; } 2 Link to comment Share on other sites More sharing options...
Wanze Posted May 21, 2013 Share Posted May 21, 2013 Glad it works for you! I think you can delete this line: $rp = $pages->get("id=$expense->id"); // the repeater page we want to update You should have the same page already in your $expense variable from the foreach loop. 1 Link to comment Share on other sites More sharing options...
renobird Posted May 22, 2013 Author Share Posted May 22, 2013 Hi Wanze, I'm not sure I follow there. Don't I need to get the actual page for the repeater before I can set the values? Link to comment Share on other sites More sharing options...
Wanze Posted May 22, 2013 Share Posted May 22, 2013 Hi Reno, When you loop through your repeater, in each iteration you already get the page. So the additional find is not needed, but I guess it does not affect performance at all because the page is loaded already: foreach ($expenses as $rp){ // changed $expense to $rp // the following line got you the same page as $expense before // $rp = $pages->get("id=$expense->id"); // the repeater page we want to update $rp->of(false); // ... 2 Link to comment Share on other sites More sharing options...
renobird Posted May 23, 2013 Author Share Posted May 23, 2013 Wanze, Ah, duh! Gotcha — sometimes I still over think things when it comes to repeaters. Link to comment Share on other sites More sharing options...
pwfans Posted August 9, 2021 Share Posted August 9, 2021 On 5/22/2013 at 4:18 AM, renobird said: Thanks Wanze, I managed to get it sorted out. $n = 1; $type = "expense$n"; $desc = "desc$n"; $amount = "amount$n"; $pm = "method$n"; $expenses = $p->travel_expense; // the repeaters foreach ($expenses as $expense){ $rp = $pages->get("id=$expense->id"); // the repeater page we want to update $rp->of(false); // update repeater fields $rp->expense_type = $input->post->$type; $rp->expense_description = $sanitizer->text($input->post->$desc); $rp->expense_amount = filter_var($input->post->$amount,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); $rp->payment_method = $input->post->$pm; // save the repeater page $rp->save(); // update counter etc $n++; $type = "expense$n"; $desc = "desc$n"; $amount = "amount$n"; $pm = "method$n"; } Is it possible to update current value while also add new item (if any) for current repeater using dynamic row ? 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