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.

Data Model Questions

[SOLVED] Adding a "virtual" value to $summary_fields


Reply

5 Posts   2518 Views

Avatar
Mad_Clog

27 March 2009 at 7:03am (Last edited: 27 March 2009 8:42pm), Community Member, 78 Posts

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

Avatar
Hamish

27 March 2009 at 8:25am (Last edited: 27 March 2009 8:26am), Community Member, 712 Posts

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.

Avatar
Mad_Clog

27 March 2009 at 8:42pm Community Member, 78 Posts

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

Avatar
hu

28 March 2009 at 3:03am Community Member, 21 Posts

or

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

with Poll.php

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

Avatar
orion

14 January 2010 at 1:03pm Community Member, 20 Posts

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?