Commit a617bd8b authored by Bernhard Baumrock's avatar Bernhard Baumrock

bugfix concat for page fields

parent 18e8cbe2
<?php
$info = [
'title' => 'RockFinder',
'version' => '1.0.7',
'version' => '1.0.8',
'summary' => 'Highly Efficient and Flexible SQL Finder Module to return page data without loading PW pages into memory',
'singular' => true,
'autoload' => false,
......
<?php namespace ProcessWire;
/**
* show contents of page reference fields
*/
/**
* bug: this does not work for custom fieldnames but makes joined pages with custom alias work
* $finder = new RockFinder("template=person, isFN=1, has_parent=7888", [
* 'lang' => 'bla', // does not work
* 'forename',
* 'surname',
* ]);
* $finder->addField('report', ['pdfs']);
* $finder->addField('report', ['charts'], ['alias'=>'test']);
*/
class RockFinderFieldPage extends RockFinderField {
class RockFinderFieldPage extends RockFinderField {
public $separator = ',';
/**
* get sql
*/
public function getSql() {
if(!$this->columns) return $this->getSqlNoColumns();
else return $this->getSqlWithColumns();
}
/**
* get sql when no additional columns are set
*/
public function getSqlNoColumns() {
$sql = "SELECT";
$sql .= "\n `$this->alias`.`pages_id` AS `pageid`";
// add all concatanated fields
$sql .= ",\n GROUP_CONCAT(`$this->alias`.`data` ORDER BY `$this->alias`.`sort` SEPARATOR '$this->separator') AS `$this->alias`";
$sql .= "\nFROM `field_{$this->name}` AS `$this->alias`";
$sql .= "\nGROUP BY `$this->alias`.`pages_id`";
return $sql;
}
/**
* get sql when additional columns are set
* we need to treat that case differently because a group_concat on the data column (the id of the joined page)
* would lead to multiple id entries in the resulting column when a joined field has multiple entries
* for example if we join the page with id 123 having a file field with files 1.jpg and 2.jpg the result would be:
* 123,123 | 1.jpg,2.jpg
*
* bug: joining a page with 2 fields having multiple items but different counts (eg filefield with 2 files
* and files field with 4 files) makes the concat result in wrong returns
*/
public function getSqlWithColumns() {
$sql = "SELECT";
$sql .= "\n `$this->alias`.`pages_id` AS `pageid`";
$sql .= ",\n `$this->alias`.`data` AS `$this->alias`";
foreach($this->columns as $column) {
if($column == 'data') continue;
$sql .= ",\n GROUP_CONCAT({$this->dataColumn($column)} ORDER BY `$this->alias`.`sort` SEPARATOR '$this->separator') AS `$column`";
}
$sql .= "\nFROM `field_{$this->name}` AS `$this->alias`";
// join all fields
......@@ -63,12 +31,8 @@ class RockFinderFieldPage extends RockFinderField {
$sql .= "\nLEFT JOIN `field_$column` AS `$column` ON `$column`.`pages_id` = `$this->alias`.`data`";
}
}
$sql .= "\nGROUP BY `$this->alias`.`pages_id`";
// if we have additional columns set we also group by the data column
// see description of getSqlWithColumns() method why we need to do this
if($this->columns) $sql .= ", `$this->alias`.`data`";
$sql .= "\nGROUP BY `$this->alias`.`pages_id`";
return $sql;
}
......
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