Hi kongondo,
Thanks for the quick reply and the welcome.
I had actually already read the topic that you mention, and this is what prompted my need: I would like to save a "stock" field for each product, but this field would have to be calculated each time a stock movement is saved, and calculating this field is precisely what takes so much time.
In the meantime, I continued looking into the problem, and I think that I came up with a solution with the following module:
class PagesSum extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Pages Sum',
'version' => 1,
'summary' => 'Adds a $pages->sum($selectorString, $fieldName) function to sum the value of a specific field over a list of pages selected by a selector string.',
'singular' => true,
'autoload' => true
);
}
public function init() {
$this->addHook('Pages::sum', $this, 'sum');
}
public function sum($event) {
$selectorString = $event->arguments(0);
$fieldName = $event->arguments(1);
// Find all the pages associated with the selector string
$pageFinder = new PageFinder();
$idQuery = $pageFinder->findIDs(new Selectors($selectorString), array('returnQuery' => true));
$idQuery->set('orderby', array());
$idQuery->set('groupby', array());
$idQuery->set('limit', array());
$stmt = $idQuery->execute();
$idArray = $stmt->fetchAll(PDO::FETCH_COLUMN);
// If no pages were found, return 0
if (count($idArray) == 0) {
$event->return = 0;
return;
}
$idString = implode(',', $idArray);
// Get the table name for the given field name
$field = $this->fields->get($fieldName);
// If no field with this name is found, return 0;
if (!$field) {
$event->return = 0;
return;
}
$tableName = $field->getTable();
// Run the SUM query
$sumQuery = new DatabaseQuerySelect();
$sumQuery->select("SUM(data)");
$sumQuery->from($tableName);
$sumQuery->where("pages_id IN ($idString)");
$stmt2 = $sumQuery->execute();
list($total) = $stmt2->fetch(PDO::FETCH_NUM);
$event->return = $total;
}
}
In my tests, it is about 60 times quicker to calculate the sum with the function $pages->sum($selectorString, $fieldName) than looping over the pages (for about 12000 pages).
I would appreciate any feedback, in case there are any other optimizations to be achieved, or any errors I haven't thought about. And I hope that this might maybe be useful to others too!