Jump to content

$page->save(); vs. $page-save($field);


Eltom
 Share

Recommended Posts

Hi folks,

it's a little bit odd for me, that a

$page->of(false);
foreach($page->images as $image) {
    $image->description = "Some Description";
}
$page->save("images");
... 

is working, while a

$page->of(false);
foreach($page->images as $image) {
    $image->description = "Some Description";
}
$page->save(); // Notice the difference here
... 

is not working (for me).  There might be a concept behind all this, so some enlightening is very appreciated!  :undecided:

Regards,

Thomas

Link to comment
Share on other sites

Does make 

$page->of(false);
foreach($page->images as $image) {
    $image->description = "Some Description";
}
$page->trackChange("images");
$page->save(); // Notice the difference here
 

a difference?

Link to comment
Share on other sites

@soma:

Yes it makes a difference and it works. trackChange() is not documented right now?!

Normally I change some fields on a page and save them with a $page->save();

Images then seems to be different than other fields... why?

Link to comment
Share on other sites

No it's not really documented unless you dig in the code, where it is documented in /wire/core/Wire.php

It's used internally to track changes for values and fields (what?) 

I'm not really sure about all the details and why it's like this when used in API. Image or file fields are very special in PW other than text fields, and I think with repeaters I noticed the a similar behavior. 

In short, an image field in PW is a Pageimage that is instanced by FieldtypeImage, and it's a FieldtypeMulti , lots of things happening there. I think along the way $page->save() isn't somehow reaching or checking the image field, as it does when a page is saved in the admin which is handles by a special context and by the ProcessPageEdit module. Or in this case using $page->save("fieldname") it takes a different route and explicit saving the field.

Looks like when modifying values for file fields it's not getting recognized for the description and the trackChange isn't happening. So I can't say for sure but maybe setting the description with $image->set("description", "the text"); would make a difference? 

Otherwise Ryan might can share some light on this.

  • Like 1
Link to comment
Share on other sites

I think this particular one might be fixed on the dev branch, as I recall hitting the same issue a couple months ago. But the intention is that you shouldn't ever have to use trackChange() yourself unless you are writing a module or doing something out of the ordinary.  

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