I have the following function which I want to use to allow voting on a page (currently for testing it just increments however it will also decrease which I'll sort out)
function ProcessVote($entryID,$action) {
$entry = DataObject::get_by_id('ThingsYouShouldEntry',$entryID);
if ($entry) {
++$entry->VoteCount;
$entry->write();
}
}
I have two classes ThingsYouShouldHolder extending BlogHolder and ThingsYouShouldEntry extending BlogEntry.
mysite\code\ThingsYouShouldEntry.php has the VoteCount field defined
class ThingsYouShouldEntry extends BlogEntry {
static $db = array(
'LinkURL' => 'Text',
'VoteCount' => 'Double',
);
static $has_one = array(
'ThingsYouShouldEntryImage' => 'Image'
);
static $allowed_children = "none";
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new TextField('LinkURL', 'Link to more info...', 'http://'),'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('VoteCount', 'Votes', '1'),'Content');
$fields->addFieldToTab('Root.Content.Image', new ImageField('ThingsYouShouldEntryImage', 'Image to show with entry'));
return $fields;
}
}
I wanted to be able to call $ProcessVote from anywhere within my templates .ss files so placed it in mysite\code\Page.php
in the class Page_Controller extends ContentController {...}
It works Ok and increments the database field by one apart from when it is called from
themes\...\templates\Layout\ThingsYouShouldHolder.ss
where the value increases by 2 not 1.
I'm lost as to why it seems to run twice from just that template file?
What am I doing wrong?