Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » write() on Sitetree does not work

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 461 Views
  • spierala
    Avatar
    Community Member
    80 Posts

    write() on Sitetree does not work Link to this post

    Hello All,

    i have a very simple counter function placed in Page_Controller in Page.php (which extends Sitetree):

    public function ilike(){
       $newCount = $this->Counter + 1;
       $this->Counter = $newCount;
       $this->write();
    }

    somehow the data is never ever updated.
    I also tried the update() function.
    does not help. I get no errors or something.
    are there any restrictions to write on a sitetree item?

    thanks in advance,
    florian

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: write() on Sitetree does not work Link to this post

    Hi Florian,

    the "write" is for a straight dataobject, but page is with versioned so you need to save and publish (or save to draft)... try somethign like...

    $this->writeToStage('Stage');
    $this->publish("Stage", "Live");

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: write() on Sitetree does not work Link to this post

    hello swaiba! thank you,
    but somehow that does not work as well.

    I saw kind of this solution in other posts but it never worked for me.

    Maybe in the worst case I have to create an DataObject just holding the count and relate it to the Page.

    cheers, Flo

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: write() on Sitetree does not work Link to this post

    oh man ,
    i moved now the function to the model class of Page.php.

    then it works:

    public function countUp(){
       $this->Counter = $this->Counter+1;
       $this->writeToStage('Stage');
       $this->publish("Stage", "Live");
    }

    i can call this model function from the controller e.g. like this:

    public function ilike(){
       $this->countUp();
    }

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