Jump to content

Resaving a page from the same variable and duplicate items in repeaters


DrQuincy
 Share

Recommended Posts

I have some code like this (simplified):

$page = new Page();

// Set template, parent, etc here

$item = $page->matrix->getNew(); // matrix is a RpeaterMatrix

$item->repeater_matrix_type = 4; // HTML
$item->html = '<p>Foo bar.</p>';

$item->save();

$page->matrix->add($item);

$page->save(); // Saving now so I can add an image

$page->image->add('foo.jpg');

$page->save();

I always thought save() was imdempotent so if you kept saving it would just overwrite the same data. But in this instance my matrix has two identical items. This works though:

$page = new Page();

// Set template, parent, etc here

$page->save();

$item = $page->matrix->getNew(); // matrix is a RpeaterMatrix

$item->repeater_matrix_type = 4; // HTML
$item->html = '<p>Foo bar.</p>';

$item->save();

$page->matrix->add($item);

$page->save('matrix'); // Saving now so I can add an image

$page->image->add('foo.jpg');

$page->save('image');

Just so I understand is the matrix difference to the other fields in that once added to the page when just $page->save() is called it will save a new once each time? I.e. it doesn‘t “forget” about the new matrix item on saving.

In general, is it better to save by explcitily passing field names as above (once you've saved the core fields)? If so, can you pass an array of fields to save()? It doesn't appear so from the docs.

I've been using PW for a while now and have always used saved and not yet come across this. ? 

Thanks!

Link to comment
Share on other sites

@DrQuincy Matrix repeaters are pages themselves. They are different than usual fields.
First you create a matrix type and add content. Then save the matrix page and then you can save your actual page.
Each time you add a matrix type a new page is created.
You can find these pages in the page tree under admin

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...