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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

SS3 onAfterWrite Browser Hangs


Go to End


5 Posts   1770 Views

Avatar
okotoker

Community Member, 50 Posts

18 July 2012 at 3:45pm

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

Forum Moderator, 5523 Posts

18 July 2012 at 8:02pm

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

Community Member, 50 Posts

19 July 2012 at 4:05am

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

Forum Moderator, 5523 Posts

19 July 2012 at 5:43pm

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

Community Member, 50 Posts

20 July 2012 at 9:29am

That did the trick. Thanks for the help.