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.

Customising the CMS /

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

Function running twice in controller


Go to End


4 Posts   1721 Views

Avatar
spenniec

Community Member, 37 Posts

8 February 2010 at 10:06pm

Edited: 08/02/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 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?

Avatar
Hamish

Community Member, 712 Posts

8 February 2010 at 10:22pm

What does ThingsYouShouldHolder.ss look like?

Avatar
spenniec

Community Member, 37 Posts

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,++)

Avatar
Hamish

Community Member, 712 Posts

10 February 2010 at 7:22am

Edited: 10/02/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.