Jump to content

Updating Repeaters via API - without removeAll()


renobird
 Share

Recommended Posts

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

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

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 25
Notice: Trying to get property of non-object in /xxxx/wire/core/PageAccess.php on line 27
Fatal 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

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";  
} 
  • Like 2
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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);
    // ...
  • Like 2
Link to comment
Share on other sites

  • 8 years later...
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

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