Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Adding a "virtual" value to $summary_fields

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

Page: 1
Go to End
Author Topic: 2410 Views
  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    [SOLVED] Adding a "virtual" value to $summary_fields Link to this post

    Hi,

    I was wondering if it is possible to add a virtual field to the $summary_fields of a dataobject.
    The situation:
    3 DataObject's: Poll, PollAwnser and PollVotes
    Now in the overview page of polls i would like to list the number of votes cast for each poll.
    (SELECT COUNT(PollID) FROM PollVote WHERE PollID = <ID>)

    Is there any way to do this in SilverStripe?
    p.s. I'm using ModelAdmin to for the UI if that's off any importance.

    Cheers,
    Geert

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: [SOLVED] Adding a "virtual" value to $summary_fields Link to this post

    Yes, this should be possible. The summary field can also be the result of an object method, so for example in your Poll object:

    // Cast method calls nicely
    static $casting = array(
       "VotesCast" => "Int"
    );

    // Return the number of votes cast in this poll
    function getVotesCast() {
       $sqlQuery = new SQLQuery(
          "COUNT(*)",
          "PollVote",
          "PollID = $this->ID"
       );
       return $sqlQuery->execute()->value();
    }

    Now you should be able to add "VotesCast" to your summary fields.

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: [SOLVED] Adding a "virtual" value to $summary_fields Link to this post

    As said on IRC before, thanks for the help.
    Works like a charm.

  • hu
    Avatar
    Community Member
    21 Posts

    Re: [SOLVED] Adding a "virtual" value to $summary_fields Link to this post

    or

    function getVotesCount() {
       return $this->PollVotes()->count();
    }

    with Poll.php

    static $has_many = array (
       'PollVotes' => 'PollVote'
    );

  • orion
    Avatar
    Community Member
    20 Posts

    Re: [SOLVED] Adding a "virtual" value to $summary_fields Link to this post

    I have installed the poll module to my site. How do I display the history of all the polls and results to a single page?

    2410 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.