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.

Data Model Questions

write() on Sitetree does not work


Reply

4 Posts   554 Views

Avatar
spierala

9 November 2011 at 11:17pm (Last edited: 9 November 2011 11:19pm), Community Member, 80 Posts

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

10 November 2011 at 12:00am Forum Moderator, 1796 Posts

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

10 November 2011 at 12:18am Community Member, 80 Posts

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

10 November 2011 at 4:52am Community Member, 80 Posts

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