Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

SS3 onAfterWrite Browser Hangs


Reply

5 Posts   640 Views

Avatar
okotoker

18 July 2012 at 3:45pm Community Member, 41 Posts

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.

Avatar
Willr

18 July 2012 at 8:02pm Forum Moderator, 5511 Posts

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

Avatar
okotoker

19 July 2012 at 4:05am Community Member, 41 Posts

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.

Avatar
Willr

19 July 2012 at 5:43pm Forum Moderator, 5511 Posts

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.

Avatar
okotoker

20 July 2012 at 9:29am Community Member, 41 Posts

That did the trick. Thanks for the help.