5121 Posts in 1527 Topics by 1119 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 695 Views |
-
Function running twice in controller

8 February 2010 at 10:06pm Last edited: 8 February 2010 10:08pm
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 definedclass 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? -
Re: Function running twice in controller

8 February 2010 at 10:22pm
What does ThingsYouShouldHolder.ss look like?
-
Re: Function running twice in controller

9 February 2010 at 10:31pm
It looks like this
<% if Level(2) %>
<ul>
<% control AllChildren %>
<li>$Title | <a href="$LinkURL" target="_blank">$LinkURL</a> | $Content | $Date.Long | $VoteCount Votes </li>
<% end_control %>
</ul>
<% else %>
$Content
<% end_if %>$ProcessVote(11,++)
-
Re: Function running twice in controller

10 February 2010 at 7:22am Last edited: 10 February 2010 7:22am
Ok, can't tell what the problem is from the template, but it is being run twice somewhere. If you add a Debug call in the function, and turn on template comments (on by default in dev mode) then you should be able to tell where it is being called from the generated source code.
| 695 Views | ||
|
Page:
1
|
Go to Top |


