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   2525 Views

Avatar
Mad_Clog

Community Member, 78 Posts

27 March 2009 at 7:03am

Edited: 27/03/2009 8:42pm

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

Community Member, 712 Posts

27 March 2009 at 8:25am

Edited: 27/03/2009 8:26am

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

Community Member, 78 Posts

27 March 2009 at 8:42pm

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

Avatar
hu

Community Member, 21 Posts

28 March 2009 at 3:03am

or

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

with Poll.php

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

Avatar
orion

Community Member, 20 Posts

14 January 2010 at 1:03pm

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?