...
 
Commits (2)
<?php
$info = [
'title' => 'RockFinder',
'version' => '1.0.5',
'version' => '1.0.6',
'summary' => 'Highly Efficient and Flexible SQL Finder Module to return page data without loading PW pages into memory',
'singular' => true,
'autoload' => false,
......
......@@ -177,9 +177,6 @@ class RockFinder extends WireData implements Module {
foreach($this->fields as $field) $sql .= $field->getJoin();
$rockfinder = $sql;
// debug
bdb($this->joinedFinders);
// join both queries
$sql = "SELECT";
$sql .= "\n `rockfinder`.*";
......@@ -221,24 +218,30 @@ class RockFinder extends WireData implements Module {
*/
private function joinedFinderJoins() {
if(!count($this->joinedFinders)) return;
bd($this->joinedFinders);
$sql = "\n\n/* joinedFinderJoins */";
foreach($this->joinedFinders as $finder) {
foreach($finder[1] as $field1=>$field2) {}
bd($field1, 'f1');
bd($field2, 'f2');
foreach($finder[1] as $field1=>$field2) {} // assign key/value
// create sql statement
$finder = $finder[0];
$sql .= "\n\n/* join finder {$finder->joinPrefix} */";
$sql .= "\nLEFT JOIN (";
$sql .= "\n " . $this->indent($finder->getSQL(), 4);
$sql .= "\n) AS `" . $finder->joinPrefix . "`";
$sql .= " ON `$finder->joinPrefix`.`{$finder->joinPrefix}_$field1` = `rockfinder`.`$field2`";
/**
* das problem ist, dass er bei den gejointen findern die felder nicht findet, weil die felder
* alle ein prefix haben. dieses prefix ist leider kacke und das muss ich anders umsetzen
* TODO für morgen...
*/
// create ON clause
// if $field2 has a dot the join is performed on an already joined table
// $finder1->join($finder2, 'contact', ['id' => 'client']);
// $finder1->join($finder3, 'referrer', ['id' => 'contact.contact_camefrom']);
if(strpos($field2, '.')) {
$field2 = str_replace('.', '`.`', $field2);
$sql .= " ON `$finder->joinPrefix`.`{$finder->joinPrefix}_$field1` = `$field2`";
}
else {
$sql .= " ON `$finder->joinPrefix`.`{$finder->joinPrefix}_$field1` = `rockfinder`.`$field2`";
}
}
return $sql;
}
......@@ -358,6 +361,8 @@ class RockFinder extends WireData implements Module {
* join another finder
*/
public function join($finder, $prefix, $fields) {
bd('join');
// parameter checks
if(!$finder instanceof RockFinder) {
throw new WireException('First parameter needs to be a RockFinder instance');
......@@ -373,14 +378,9 @@ class RockFinder extends WireData implements Module {
// set the join prefix for the joined finder
$finder->joinPrefix = $prefix;
// add this join to the array
// add join to array
$this->joinedFinders[] = [$finder, $fields];
// bd($field1, 'field1');
// bd($field2, 'field2');
return $this->getSQL();
}
/**
......
......@@ -49,6 +49,57 @@ By default uses the id column, but another one can be specified:
# Advanced Usage
## Joins
It is possible to join multiple finders. This is useful whenever you have single
page reference fields and want to show properties of the referenced page. A simple
example could be this join:
```php
$finder1 = new RockFinder('template=rockproject', ['title', 'rockproject_client']);
$finder2 = new RockFinder('template=rockcontact', ['title']);
// join finder
$finder1->join($finder2, 'contact', ['id' => 'rockproject_client']);
```
The syntac is like this:
```php
$baseFinder->join($joinedFinder, 'joinedFinderAlias', ['fieldNameOfJoinedFinder' => 'fieldNameOfBaseFinder']);
```
A more advanced example is this one, joining three finders. Notice that `$finder3`
is manually joined on a column of `$finder2` (having alias `contact`). You can
achieve this by providing not only the field name (then it would join to the base
finder) but also providing the finder-alias and the fieldname manually.
You have to use this syntax for your "fieldname": `{alias}.{alias}_{fieldname}`.
```php
$finder1 = new RockFinder('template=rockproject', [
'title',
'rockproject_client',
]);
$finder2 = new RockFinder('template=rockcontact', [
'title',
'rockcontact_camefrom',
]);
$finder3 = new RockFinder('template=rockcontact', [
'title',
]);
// join finders
$finder1->join($finder2, 'contact', ['id' => 'rockproject_client']);
$finder1->join($finder3, 'referral', ['id' => 'contact.contact_rockcontact_camefrom']);
return $finder1;
```
![complex join](screenshots/join.png)
## Custom SQL: Aggregations, Groupings, Distincts...
You can apply any custom SQL with this technique:
......