Jump to:

23380 Posts in 18311 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » onBeforeWrite question

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

Page: 1 2
Go to End
Author Topic: 5095 Views
  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    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?

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    ...do i have tp put this in?

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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    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?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: onBeforeWrite question Link to this post

    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

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    Hi banal

    That has worked!

    Thanks very much for your help with this

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

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

    5095 Views
Page: 1 2
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.