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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

onBeforeWrite question


Reply

14 Posts   5231 Views

Avatar
zim

14 November 2009 at 7:20am Community Member, 135 Posts

Just noticed that even though in CMS seems to be working on published site the changes do not appear?

I read this
"
you should never directly to the live tables since it will lead to syncing problems when you re-publish a page and erase your data...
"

Would this be the problem?

Avatar
zim

14 November 2009 at 7:43am Community Member, 135 Posts

...do i have tp put this in?

public function init() {
parent::init();
}

Avatar
zim

14 November 2009 at 9:40am Community Member, 135 Posts

I think using the raw sql method will not reflect changes that are made in CMS because somehow the update statment though visible in CM is not occuring on published site...

Is there any way to rectify this? Does this make sense?

Avatar
banal

14 November 2009 at 10:07am (Last edited: 14 November 2009 10:12am), Community Member, 901 Posts

Hi zim

I guess the problem exists because a Page is usually stored in two different tables, one for Live and one for Draft mode. And yes, raw DB querys usually target only a single table.
If you're feeling uncomfortable with raw SQL queries, you could also get a set of your pages, iterate over it and set the IsMainStory flag.

Something like this:

function onAfterWrite() {
   // call parent first
   parent::onAfterWrite();
   
   if($this->IsMainStory == false)
      return;
      
   
   $set = DataObject::get($this->ClassName);
   if(!$set)   
      return;

   foreach($set as $item){
      if($item->ID != $this->ID){
         $item->IsMainStory = false;
         $item->write();
      }
   }
}

If that doesn't work for you, please post the code of your class at http://pastie.org/ and provide us a link to it so we can have a look.

Update Oh.. important. You should probably use onAfterWrite insted of onBeforeWrite, since only then you can be sure that the flag and ID has been written to DB! I changed the code above accordingly

Avatar
zim

14 November 2009 at 10:17am Community Member, 135 Posts

Hi banal

That has worked!

Thanks very much for your help with this :)

Avatar
zim

14 November 2009 at 10:20am Community Member, 135 Posts

...I really need to improve my PHP knowledge. Thanks again :)