Jump to content

Fields and Files Saved to Page Are Not in $page Object


ethanbeyer
 Share

Recommended Posts

This is a strange error I am totally unsure how to fix!

It started when I was trying to get a file from $page->files.
I know that there is a file in a field called document_Files. But $page->files returns null.

So I popped into Tracy Debugger and wrote this code:
 

$fg = $this->wire('fieldgroups')->get('document');
$t = $this->wire('templates')->get('document');
$page = $this->wire('pages')->get(1624);
dump($fg, $t->fields, $page->fields, $page->files, $page->document_Files, $page->data);

Resulting in 6 Dumps:

$fg, $t->fields, $page->fields all result in the same object, which is to be expected. (fieldgroup):

Fieldgroup {#173
  +"count": 4
  +"items": array:4 [
    1 => "title"
    199 => "document_customizableCheckbox"
    197 => "document_Files"
    200 => "document_thumbnails"
  ]
}

 

But then it gets strange with $page->files

null

...and $page->document_Files.

Pagefile {#267
  +"changes": array:1 [
    0 => "formatted"
  ]
  +data: array:6 [
    "basename" => "example.pdf"
    "description" => ""
    "tags" => ""
    "formatted" => true
    "modified" => 1497976118
    "created" => 1497976118
  ]
}

I would expect those two to match. But obviously they don't. But then it gets weirder, still!

 

$page->data should have four elements in the array - one for each field in the Fieldgroup - but two don't show up:

array:2 [
  "title" => "Introduction"
  "document_Files" => Pagefiles {#264
    +"count": 1
    +"items": array:1 [
      "payroll_vault_brochure_markbeaton_2016_12_01p.pdf" => "payroll_vault_brochure_markbeaton_2016_12_01p.pdf"
    ]
  }
]

 

Also, in the $page->data array, document_Files (the field) outputs as a Pagefiles class, whereas getting the field directly only returns a Pagefile. I don't even know where to start! Has anyone else had something like this happen?

I have looked at the database starting with the template.
I can confirm it has the correct fieldgroup_id, and that fieldgroup has all the correct fields. The page in question also is the correct template.

Link to comment
Share on other sites

@tpr I am not sure I know what you mean, but I changed the code within Tracy Debugger to

$fg = $this->wire('fieldgroups')->get('document');
$t = $this->wire('templates')->get('document');
$example = $this->wire('pages')->get(1624);
dump($fg, $t->fields, $example->fields, $example->files, $example->document_Files, $example->data);

...and all the dumps were the same as when the Page variable was named $page.

Link to comment
Share on other sites

4 hours ago, ethanbeyer said:

I know that there is a file in a field called document_Files. But $page->files returns null.

$page->files means "the value of a field on the page named files" - it isn't a special method that somehow gets any files you have added to the page. You have named your files field "document_Files" so you get its value with $page->document_Files. If you have no field named "files" then trying to get a field value by that name will return null.

  • Like 1
Link to comment
Share on other sites

Oh my gosh - have I been reading these docs wrong all this time?

http://processwire.com/api/ref/pagefiles/

hahahahaha I thought $page->files was an attribute that got set at some point and was a Pagefiles array of all files added to that page...

Well that definitely solves one thing!! Thank you, @Robin S!

 

I'm still very confused why some of the fields don't show up as a part of the $page->data array, though.

Link to comment
Share on other sites

41 minutes ago, ethanbeyer said:

I'm still very confused why some of the fields don't show up as a part of the $page->data array, though.

I have witnessed that myself from time to time, but have never worried about it because it didn't seem to affect anything. If you can reproduce it on a clean installation (to rule out any module issues) then might be worth raising a GitHub issue.

Link to comment
Share on other sites

$page->data is a "remnant" of subclassing the WireData class. It's there to store any value assigned to a page:

$p = $pages->get(1)
$p->someRandomWhatever = 'Hallo';
echo $p->someRandomWhatever; // Hallo
echo $p->data; // {'someRandomWhatever' => 'Hallo', …}

Some fields do use this, but especially the native fields (parent, children, …) or others are implemented in different ways. It's at least not in any way specific to fields.

  • Like 3
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...