Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

write() on Sitetree does not work


Go to End


4 Posts   1754 Views

Avatar
spierala

Community Member, 80 Posts

9 November 2011 at 11:17pm

Edited: 09/11/2011 11:19pm

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

Avatar
swaiba

Forum Moderator, 1899 Posts

10 November 2011 at 12:00am

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");

Avatar
spierala

Community Member, 80 Posts

10 November 2011 at 12:18am

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

Avatar
spierala

Community Member, 80 Posts

10 November 2011 at 4:52am

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();
}