Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 onAfterWrite Browser Hangs

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 625 Views
  • okotoker
    Avatar
    Community Member
    41 Posts

    SS3 onAfterWrite Browser Hangs Link to this post

    I am doing something pretty simple

    public function onAfterWrite( ){
             
          $pageID = $this->LinkPageID;
          if($pageID){
             $LinkedPage = Page::get()->filter(array('ID'=>$pageID))->first();
             $LinkedPage->LinkPageID = $this->ID;
             $LinkedPage->write();
           parent::onAfterWrite();   
          }
          
       }

    I can see the update in the database so its working and sometimes it goes though just fine but most of the time the browser just hangs and then gets a 500 error. The fact that sometimes it works fine and that I can see the updates in the database has me boggled.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: SS3 onAfterWrite Browser Hangs Link to this post

    It isn't stuck in a loop is it? i.e LinkedPage->write() triggers a new onBeforeWrite() which then fires a new onBeforeWrite.

  • okotoker
    Avatar
    Community Member
    41 Posts

    Re: SS3 onAfterWrite Browser Hangs Link to this post

    Ok so I created a test were it created new records instead of just changing info and it was creating 5 new records so it is looping. I am using onAfterWrite here and not onBeforeWrite.

    So how do I update new information without creating the loop? I tried all sorts of things but always got the loop. So I upon saving the current page updating information on another page as well is what I am trying to accomplish.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: SS3 onAfterWrite Browser Hangs Link to this post

    Try not calling write() as that will trigger the loops again. You can use something like a direct DB::query() to update your database rather than using the ORM.

  • okotoker
    Avatar
    Community Member
    41 Posts

    Re: SS3 onAfterWrite Browser Hangs Link to this post

    That did the trick. Thanks for the help.

    625 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.