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

Using MySQL SELECT SUM


Reply

4 Posts   2711 Views

Avatar
novaweb

26 March 2009 at 11:57am Community Member, 115 Posts

Is there a SilverStripe function that has similar behavior to using the

SELECT SUM(NumVisit) FROM 'Member';

statement?

Cheers

Avatar
Ben Gribaudo

27 March 2009 at 2:43am Community Member, 181 Posts

Hi!

If you can load the data into a DataObjectSet, you can use that class' Count() method.

$submissions = DataObject::get('BrowserPollSubmission');
$total = $submissions->Count();


(Example from: http://doc.silverstripe.org/doku.php?id=tutorial:3-forms#showing_the_poll_results)

Docs:
http://api.silverstripe.org/sapphire/model/DataObjectSet.html#Count

Hope that helps,
Ben

Avatar
Hamish

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

Not quite, Count will return the number of members, not the sum of their visits.

There is an SQL query object that can do this sort of thing:

$sqlQuery = new SQLQuery(
   "SUM(NumVisit)", // Select
   "Member", // From
   "" // Where (optional)
);
$totalVisits = $sqlQuery->execute()->value();

Avatar
Ben Gribaudo

27 March 2009 at 9:07am Community Member, 181 Posts

Oops! I'm not sure why I thought subvert was referring to SQL Count instead of Sum. Thanks for catching that, Hamish!