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


Go to End
Reply


14 Posts   5365 Views

Avatar
zim

Community Member, 135 Posts

14 November 2009 at 7:20am

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

Community Member, 135 Posts

14 November 2009 at 7:43am

...do i have tp put this in?

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

Avatar
zim

Community Member, 135 Posts

14 November 2009 at 9:40am

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

Community Member, 901 Posts

14 November 2009 at 10:07am

Edited: 14/11/2009 10:12am

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

Community Member, 135 Posts

14 November 2009 at 10:17am

Hi banal

That has worked!

Thanks very much for your help with this :)

Avatar
zim

Community Member, 135 Posts

14 November 2009 at 10:20am

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

Go to Top