Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » onAfterWrite not giving correct link()

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: 989 Views
  • otherjohn
    Avatar
    Community Member
    125 Posts

    onAfterWrite not giving correct link() Link to this post

    Hi all,
    I am trying to send blog posts to an api but when I try to get the $this->Link() in my onAfterWrite function, it returns the link that is automatically created, not the saved one.

    For example, if its a blog post Link returns "/blog/new-blogentry/" instead of "/blog/my-set-link/"
    if its a page it returns "/new-page" instead of "/my-page-link"

    Any idea what I need to do to get the correct link?

    Also "$this->obj('Content')->NoHTML();" is coming up blank...

    I have the following in my Page.php file.

    public function onAfterWrite() {
             parent::onAfterWrite();
    .... $this->Link();
    ... $this->obj('Content')->NoHTML();
    ...
    ...
    }

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: onAfterWrite not giving correct link() Link to this post

    I'd do a debug of $this and see what it's got.

    I think I may have used the ID to do a DataObject::get_by_id of the very same object in an onAfterWrite to get the db contents... even though this might not be best... it might work....

  • otherjohn
    Avatar
    Community Member
    125 Posts

    Re: onAfterWrite not giving correct link() Link to this post

    Very odd. I am not getting all the updated info when doing it the original nor DataObject::get_by_id
    for example "title" stays New BlogEntry instead of the new title I wrote.

    perhaps I need to call the write function first?

  • HARVS1789UK
    Avatar
    Community Member
    14 Posts

    Re: onAfterWrite not giving correct link() Link to this post

    Swaiba is correct that doing a DataObject::get_by_id('Some_Object', $this->ID, false); will solve the problem.

    This is because not all database calculated values (e.g. LastEdited) are not actually returned to the current instance of our object ($this) until AFTER the onAfterWrite has finished.

    For example:

    - $this is our current instance of object_x
    - In the onBeforeWrite() function our DB table column which represents object_x HAS been updated and things like the LastEdited field have been set
    - However, these DB generated values (I have not tested it to be sure but id imagine Created, LastEdited and ClassName are the values in question) are not returned to our instance of objext_x ($this) until AFTER the onAfterWrite has finished.
    - Therefore they cannot be used/are old data when used in the onBeforeWrite()

    Using

    $obj = DataObject::get_by_id('Some_Object', $this->ID, false);
    if($obj->SomeField == 'Some Value') {
    // Do Stuff
    }

    rather than

    if($this->SomeField == 'Some Value') {
    // Do Stuff
    }

    solves the issue as it effectivly manually refreshes/updates your current instance of object_x

    EDIT: Having implemented my own fix and still having issues with my returned LastEdited DATETIME I have realised something very interesting. Many of you may well know (as it is in the docs) that DataObject::get_one has a cached query option which defaults to true. DataObject::get_by_id() has absolutely no mention of the caching option in the docs anywhere I can see, but guess what it uses to do its lookup...a modified get_one()! Meaning that DataObject::get_by_id also uses a cached query by default! There is an optional third parmeter to overwrite this and do a fresh query, however it doesn't seem to be documented anywhere. See my line above. The third parameter was integral to my code working as using a cached query returned me outdated data!

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