Commit 8d04a8ce authored by Bernhard Baumrock's avatar Bernhard Baumrock

add custom select statement option

parent c0b6bd2b
<?php
$info = [
'title' => 'RockFinder',
'version' => 103,
'version' => '1.0.4',
'summary' => 'Highly Efficient and Flexible SQL Finder Module to return page data without loading PW pages into memory',
'singular' => true,
'autoload' => false,
......
......@@ -23,6 +23,9 @@ class RockFinder extends WireData implements Module {
// only create the query once
public $sql;
// array of custom select statements
private $selects = [];
public function __construct($selector = '', $fields = []) {
$this->selector = $selector;
......@@ -82,6 +85,15 @@ class RockFinder extends WireData implements Module {
return $field;
}
/**
* add a select statement to SQL query
* if you are using this method to aggregate data (such as sum() etc)
* note that this might not be as efficient as doing a sum() and group by ... on the resulting SQL
*/
public function addSelects($statements = []) {
$this->selects = array_merge($this->selects, $statements);
}
/**
* add multiple fields via array
*/
......@@ -139,6 +151,12 @@ class RockFinder extends WireData implements Module {
// start sql statement
$sql = "SELECT\n `pages`.`id` AS `id`";
foreach($this->fields as $field) $sql .= $field->getJoinSelect();
// add all select statements
foreach($this->selects as $alias=>$statement) {
$sql .= ",\n ($statement) AS $alias";
}
$sql .= "\nFROM\n `pages`";
foreach($this->fields as $field) $sql .= $field->getJoin();
$rockfinder = $sql;
......@@ -294,6 +312,7 @@ class RockFinder extends WireData implements Module {
$info['fields'] = $this->fields;
$info['closures'] = $this->closures;
$info['debuginfo'] = $this->debuginfo;
$info['selects'] = $this->selects;
return $info;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment