Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Function running twice in controller

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

Page: 1
Go to End
Author Topic: 844 Views
  • spenniec
    Avatar
    Community Member
    37 Posts

    Function running twice in controller Link to this post

    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?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Function running twice in controller Link to this post

    What does ThingsYouShouldHolder.ss look like?

  • spenniec
    Avatar
    Community Member
    37 Posts

    Re: Function running twice in controller Link to this post

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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Function running twice in controller Link to this post

    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.

    844 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.