Jump to content

Problem: saving repeater item from one page to another


theoretic
 Share

Recommended Posts

Thanks everyone for a perfect product!

Got a problem during development of a website which has two different templates sharing the same "suboffers_prices" repeater field: the "offer" template and the "order" template. The idea is to take a single repeater item from offer page and to save it as a repeater item to a newly-created order page.

Getting a single repeater is not complicated:

$suboffers_prices = $pages->get("uid={$input->post->suboffers_prices_uid}");

But saving that suboffers_prices to a new order page appeas to be not that easy:

    $newOrder->suboffers_prices = $suboffers_prices;
    $newOrder->save(); //doesn't save the repeater

...another try...

    $newOrder->suboffers_prices = $suboffers_prices;
    $newOrder->suboffers_prices->save(); //in fact saves nothing
...
    $newOrder->save(); //doesn't save the repeater

...and another...

    $newOrder->suboffers_prices->getNew();
    //$newOrder->suboffers_prices->of(false); //not callable in this context, PW crashes with error
    $newOrder->suboffers_prices = $suboffers_prices;
    $newOrder->suboffers_prices->save(); //again saves nothing
...
    $newOrder->save(); //again no repeater saved

Tried some other approaches, still no result. Would like to get any advice on the best practice for saving a single repeater item to another page. Thanks in advance!

Link to comment
Share on other sites

Not certain, but I think you may need to iterate through each of the repeater items subfields, adding them to the new repeater item.

Also, take a look at this: 

In a hurry, so not sure if you are perhaps following that correctly or not.

 

 

Link to comment
Share on other sites

OK, have some hours spent to resolve the problem. Conclusions:

  1. It's possible to clone a repeater item using API,..
  2. ...but it's a delicate procedure which can be performed in the only correct way. Trying to use alternative approaches may even corrupt your database (thanks again to the PW creators for making a very simple and straightforward DB structure which can easily be healed if corrupted!)
  3. My way to clone the repeater item from one page to another:
     
    //Creating a new page which will hold the repeater clone
    $newPage= new Page();
    $newPage->of(false);
    $newPage->template = 'my_template_with_repeater_field';
    //Setting some more fields skipped
    ...
    //Getting the original repeater item
    $repeaterItem = $pages->get("some_selector=$some_value");
    
    //Saving the page before cloning the repeater -- it's very important!
    $newPage->save();
    
    //OK let's clone that repeater item now!
    $repeaterItemClone = $newPage->repeater_field->getNew(); //the repeater clone is already created and saved to the database
    //Cloning the repeater fields
    $repeaterItemClone->field1 = $repeaterItem->field1;
    $repeaterItemClone->field2 = $repeaterItem->field2;
    //Cloning some more fields skipped
    ...
    //Saving the modified clone
    $repeaterItemClone->save();

     

  4. Maybe it could be useful to create a PW module for cloning repeater items.

Hope this will be useful to someone ;)

 

Link to comment
Share on other sites

4 hours ago, theoretic said:

//Cloning the repeater fields $repeaterItemClone->field1 = $repeaterItem->field1; $repeaterItemClone->field2 = $repeaterItem->field2; //Cloning some more fields skipped

So rather than specifying each field, this is why I suggested looping through them.

 

4 hours ago, theoretic said:

Maybe it could be useful to create a PW module for cloning repeater items.

This seems like a perfect idea for an action for the AdminActions module (https://processwire.com/talk/topic/14921-admin-actions/) action. Are you interested in putting one together, or would you like me to?

  • Like 1
Link to comment
Share on other sites

@theoretic - I have added it as a new action in my module. However I am not sure it will really help with your current need - it provides a GUI for copying/overwriting repeater items from one page to another. Hopefully it might be useful for you in other circumstances though.

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