Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Can't programmatically reference a new file in a file field in a new page #86

Open
JerikoOne opened this issue Sep 14, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@JerikoOne
Copy link

What I want to achieve is programmatically update or add new pages, add a file to these pages if it doesn't exist yet and reference it in a files field. Blueprints for the page and the file are correctly set up.

$pageData = [...];

if( $pageDoesNotExist == true ) :
  $page = $parent->createChild([ 'slug' => ..., 'template' => 'template', 'content' => $pageData ]);
  $page = $page->changeStatus('listed');
else:
  $page = $page->update( $pageData );
endif;

if( $fileDoesNotExist == true ) :
  $file = $page->createFile(...) // Array with required properties and such
  $page = $page->update([ 'files_field' => $file->id() ]);
endif;
  • Auto ID is added to the page and file as expected.
  • If the page already exists everything works as expected, the file get's referenced in the files field from that page.
  • If the page is newly created however the file doesn't get referenced in the files field from that page.
  • If I remove the plugin everything works as expected, also with newly created pages.

I can only assume that it's some sort of caching issue with the SQlite database, however I couldn't figure it out myself.

@JerikoOne
Copy link
Author

Small update: basically I can't update a newly programmatically created page in the same call, it's not limited to files or the files field.

@bnomei bnomei self-assigned this Oct 6, 2022
@bnomei bnomei added the bug Something isn't working label Oct 6, 2022
@bnomei
Copy link
Owner

bnomei commented Oct 6, 2022

sorry for the late response i was on vacation.

the sqlite database only stores information about the id, template and paths but nothing about about the pages data so i am very sure its not a caching issue.

most likely this is caused by how autoid writes the id to the content file using a hook. it should not however block updating further calls - at least there is nothing i can do as a plugin developer here.

my suggestion would be you try to actively refetch the page object instead of relying on the return value. i know it looks a bit silly to do $page = page($page->id()); but i think its worth a try.

$pageData = [...];

if( $pageDoesNotExist == true ) :
  $page = $parent->createChild([ 'slug' => ..., 'template' => 'template', 'content' => $pageData ]);
  $page = $page->changeStatus('listed');
else:
  $page = $page->update( $pageData );
endif;

$page = page($page->id());

if( $fileDoesNotExist == true ) :
  $file = $page->createFile(...) // Array with required properties and such
  $page = $page->update([ 'files_field' => $file->id() ]);
endif;

@bnomei
Copy link
Owner

bnomei commented Oct 6, 2022

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants