monollonom Posted April 15, 2021 Share Posted April 15, 2021 (edited) (I edited the title to reflect my findings down below, I'm leaving the original post as is though) Hi, This is the first time I'm using LazyCron so maybe I did something wrong but basically when the hook function is triggered and does its thing to the homepage, all images within that page disappear from the database, while remaining in the file system. When I try to re-upload the same image it tells me "Refused file X because it is already on the file system and owned by a different field" (couldn't find that other field though). Has anybody encountered something like this before ? Here is my hook, from ready.php: $wire->addHook("LazyCron::everyHour", function() { $home = wire("pages")->get(1); $home->of(false); $nextMonth = strtotime("next month"); $newEvents = wire("pages")->find("template=event, end_date>=today, start_date<$nextMonth, limit=10, sort=start_date, sort=end_date, sort=title"); foreach($home->events as $event) { if (!$newEvents->has($event)) { $home->events->remove($event); } } foreach($newEvents as $new) { if (!$home->events->has($new)) { $home->events->add($new); } } $home->save(); }); $home->events is a Page Reference field. The idea is to update the field with the ten closest events and allow the editor to sort them in the admin. Thank you ! Edited April 16, 2021 by monollonom Changed title of the topic Link to comment Share on other sites More sharing options...
Robin S Posted April 15, 2021 Share Posted April 15, 2021 After you get the home page you should set its output formatting state to false because you are about to manipulate and save it. $home = wire("pages")->get(1); $home->of(false); Link to comment Share on other sites More sharing options...
monollonom Posted April 15, 2021 Author Share Posted April 15, 2021 Yes at first I did set the output formatting to "false" but wanted to try and see how it goes without it, with no luck. I'll edit my post though, thanks. (also for completeness, I'll try again with of(false) just to be 100% sure) Link to comment Share on other sites More sharing options...
monollonom Posted April 16, 2021 Author Share Posted April 16, 2021 (edited) After some more testing, it looks like it's not because of the hook or the LazyCron but because of the code being executed by a guest. Edit: it's actually just the fact that I save the homepage (from a guest session) that makes the images disappear. I have access control toggled on for the homepage template but I thought API calls would be respected, am I wrong ? Edited April 16, 2021 by monollonom Link to comment Share on other sites More sharing options...
monollonom Posted April 16, 2021 Author Share Posted April 16, 2021 In the end it seems to work if I specifically save the Page Reference field: $home->save("events"); I'm still not sure though why saving the page from a guest session would break the link between images and their field... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now