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.

Data Model Questions /

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

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


Go to End


5 Posts   3742 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?