Jump to:

23486 Posts in 18995 Topics by 2878 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: 5180 Views
  • zim
    Avatar
    Community Member
    135 Posts

    onBeforeWrite question Link to this post

    In my CMS I have a page type News_Page.php.

    in it I have a checkbox 'IsMainStory' which I check to make the story the headline story at top of holder page - and is thus displayed differnetly.

    The problem is that as more and more news pages are added the 'IsMainStory' is still checked on other stories.

    I want to check if the 'IsMainStory' is ticked on page I am about to save... and if it is I want to uncheck all other news page stories 'IsMainStory' checkbox.

    Does this make sense? Does anyone know the correct way to do this. I believe I should use the 'onBeforeWrite ()' method.

  • juandavidgc
    Avatar
    Community Member
    36 Posts

    Re: onBeforeWrite question Link to this post

    Hi Zim!

    The correct use of onBeforeWrite is:

    http://doc.silverstripe.org/doku.php?id=datamodel#onbeforewrite

    You can use it in your News_Page.php, something like this:

    class News_Page extends Page {
       
       public static $db = array(
    "IsMainStory" => "Boolean"
       );
       
       public static $has_one = array(
       );

    function onBeforeWrite() {
    if( $this->IsMainStory )
    //IsMainStory is checked
    else
    //IsMainStory is not checked
    parent::onBeforeWrite();

    }
       
    }

    class News_Page_Controller extends Page_Controller {
    public function init() {
          parent::init();
       }
    }

    I think this could run fine. Try it.

    Good luck!

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    sorry this is probably basic question

    but how do i actually make IsMainStory=false on all the other records?

    do i put it in here

    function onBeforeWrite() {
    if( $this->IsMainStory )
    //IsMainStory is checked
    else

    IsMainStory=false

    parent::onBeforeWrite();

    }

    I am a bit confused?

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    .. in fact it would be in the first bit? am i on the right lines?

    function onBeforeWrite() {

    if( $this->IsMainStory )

    //IsMainStory is checked so uncheck it before checking new one?

    IsMainStory=false //is this the right syntax?

    else

    parent::onBeforeWrite();

    }

  • juandavidgc
    Avatar
    Community Member
    36 Posts

    Re: onBeforeWrite question Link to this post

    the right sintax is:

    $this->IsMainStory=false

    you have to use $this-> before IsMainStory, because IsMainStory is an attribute of the Class News_Page.

    Good luck!

  • banal
    Avatar
    Community Member
    901 Posts

    Re: onBeforeWrite question Link to this post

    The onBeforeWrite method is only called on the DataObject that is about to be saved. It won't be called on all other pages automatically, so you have to take care of that yourself. Your problem can easily be solved with an SQL query.. here's an example code:

    function onBeforeWrite() {
       // call parent first
       parent::onBeforeWrite();
       
       // check if MainStory is true
       if( $this->IsMainStory ){
          // if yes, make sure no other story is main-story
          // we use a raw sql query here to set all IsMainStory to 0
          DB::query('UPDATE ' . $this->ClassName . ' SET IsMainStory=0');
       }
    }

    I assumed, that you have a table with a the same name as your Class in the DB (NewsPage?). If yes, the provided code should work out of the box. If that's not the case, find out in which table the IsMainStory field is stored and fill in the table name here:

    DB::query('UPDATE MyTable SET IsMainStory=0');

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    dude this is perfect..

    It is setting all IsMainStory to 0.

    However It is not setting current IsMainStory to 1.

    Would it just be

    function onBeforeWrite() {
    // call parent first
    parent::onBeforeWrite();

    // check if MainStory is true
    if( $this->IsMainStory ){
    // if yes, make sure no other story is main-story
    // we use a raw sql query here to set all IsMainStory to 0
    DB::query('UPDATE ' . $this->ClassName . ' SET IsMainStory=0');
    DB::query('UPDATE ' . $this . ' SET IsMainStory=1);
    }
    }

  • zim
    Avatar
    Community Member
    135 Posts

    Re: onBeforeWrite question Link to this post

    Got it working with this

    function onBeforeWrite() {
       
          
        // check if MainStory is true
        if( $this->IsMainStory ){
           // if yes, make sure no other story is main-story
           // we use a raw sql query here to set all IsMainStory to 0
           DB::query('UPDATE ' . $this->ClassName . ' SET IsMainStory=0');
          
           DB::query('UPDATE ' . $this . ' SET IsMainStory=1 WHERE ID=' . $this->ID);

          
          

        }
       
        // call parent first
        parent::onBeforeWrite();
       
       }

    Thanks for your help people

    5180 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.