Juergen Posted January 13, 2017 Share Posted January 13, 2017 Hello @ all, I need to store the number of children in an integer field in the parent page after the children were created. I use the ready.php for the code: $pages->addHookAfter("saved", function($event) { $page = $event->arguments[0]; if ($page->template == 'events') { if (($page->numChildren) > 0) { $page->subdatesnumber = $page->numChildren; $page->save(); } } }); I always get a blank page after save with this piece of code. I have tried several variations but without success. events = template name of the parent page subdatesnumber = integer field in the parent page where the number of children should be stored (fe 4) Workflow: After saving of the parent page some child pages will be created. After they were created the number of children should be stored in the field "subdatesnumber" which is in the parent page. Does anyone know what could be wrong with the code? Best regards Link to comment Share on other sites More sharing options...
kongondo Posted January 13, 2017 Share Posted January 13, 2017 (edited) By blank page I take it you mean exactly that...nothing shows. You should be seeing an error about memory exhausted or Apache should crash. Why? Coz you are caught in an endless loop by the looks of the code. You save the number of children, and since that is also a save action, the hook fires again, and you end up in a perpetual loop. See illustration in your code below: $pages->addHookAfter("saved", function($event)// line #1 { $page = $event->arguments[0]; if ($page->template == 'events') { if (($page->numChildren) > 0) { $page->subdatesnumber = $page->numChildren; $page->save();// this will end up going back to line #1....and so on and on and on...... } } }); Try the suggestion here by @apeisa. Use addHookBefore and remove $page->save(); in you hook function. Edited January 13, 2017 by kongondo possible solution Link to comment Share on other sites More sharing options...
Juergen Posted January 13, 2017 Author Share Posted January 13, 2017 AddHookbefore doesnt work because the number of the children will not be stored. I guess that the children were not created at the time of the Hook, so numChildren doesnt work. Link to comment Share on other sites More sharing options...
kongondo Posted January 13, 2017 Share Posted January 13, 2017 (edited) Maybe if we could see some code.... Edit Btw, will the number of children keep on increasing or are they created only once? Will the children decrease? i.e. get deleted at some point? Edited January 13, 2017 by kongondo Link to comment Share on other sites More sharing options...
Juergen Posted January 13, 2017 Author Share Posted January 13, 2017 As you can see the children were created with an addHookBefore "saveReady" hook //creation of children $pages->addHookBefore('saveReady', function($event) { $page = $event->arguments[0]; ....some code logic foreach ($periods as $key => $item) { // create new page $k = new Page(); $k->template = 'single-event'; // set template // Copy page fields from parent page to newly created child pages $k->title = $page->title; $k->eventtype = $page->eventtype; $k->eventstatus = $page->eventstatus; $k->participantmaxnumber = $page->participantmaxnumber; $k->eventmaxstatus = $page->eventmaxstatus; $k->notifiable = $page->notifiable; ...... $k->save(); } }); This code works! I have shortened the code because at the beginning there were only some date calculations for recurring events. Then in the ready.php I have tried to store the number of the children in the subdatesnumber field. // Add number of subevents to the subeventnumber field $pages->addHookBefore("saved", function($event) { $page = $event->arguments[0]; if ($page->template == 'events') { if (($page->numChildren) > 0) { $page->subdatesnumber = $page->numChildren; } } }); Link to comment Share on other sites More sharing options...
kongondo Posted January 13, 2017 Share Posted January 13, 2017 (edited) Why can't those code be combined? The example below works for me. public function init() { $this->pages->addHookBefore("saveReady", $this, "hookBefore"); } public function hookBefore(HookEvent $event) { // make sure we are on the right PARENT page $page = $event->arguments[0]; if($page->template !='some-template') return; // create new child page(s) $p = new Page(); $p->template = 'some-child-template'; $p->parent = $page; $p->title = 'New Child Page'; $p->save(); // set parent's number of children to its field 'number_of_children' // either method works. first one should be faster // @note: we DO NOT save the parent page here $page->number_of_children = $page->numChildren;// includes visible and non-visible children #$page->number_of_children = count($page->children); } Edited January 13, 2017 by kongondo 2 Link to comment Share on other sites More sharing options...
Juergen Posted January 13, 2017 Author Share Posted January 13, 2017 You are right: Combining was the solution - now it works 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