Jump to:

23450 Posts in 18899 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » check first write action and onAfterWrite method

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
Go to End
Author Topic: 1710 Views
  • Myrdhin
    Avatar
    Community Member
    67 Posts

    check first write action and onAfterWrite method Link to this post

    Hello,

    I would like to know if it's possible to check if a writing action is the first in onAfterWrite method ? I know it's possible in onBeforeWrite method with a test on the existance of the ID :

    function onBeforeWrite() {
    parent::onBeforeWrite();

    if (!$this->ID) {
    // first write
    }
    else {
    // next write when updates
    }
    }

    But, in onAfterWrite ?

    Thanks.

  • carlos
    Avatar
    Community Member
    41 Posts

    Re: check first write action and onAfterWrite method Link to this post

    salut

    so you trying to find if the DataObject you just wrote is the first in the DB?

    function onAfterWrite(){
    parent::onAfterWrite();
    $list = DataObject::get($this->ClassName);
    return $list->Count();
    }


    this will return number of DataObject

    anyway, you don't need to check if you writing a new DataObject or just updating, th framework does it for you.

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: check first write action and onAfterWrite method Link to this post

    Hi - the first thing that comes into my mind, without delving into any code, is - if you've check it in onBeforeWrite, you can easily save the result in a property, and check the value of that property in onAfterWrite..

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