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

Avatar
novaweb

Community Member, 115 Posts

26 March 2009 at 11:57am

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

SELECT SUM(NumVisit) FROM 'Member';

statement?

Cheers

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 March 2009 at 2:43am

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

Community Member, 712 Posts

27 March 2009 at 8:42am

Edited: 27/03/2009 8:43am

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

Community Member, 181 Posts

27 March 2009 at 9:07am

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