1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-08 15:57:01 +02:00

Update PageFinder so that joinType ('join' or 'leftjoin') can be modified by the fieldtype when/if needed

This commit is contained in:
Ryan Cramer
2024-03-01 15:55:32 -05:00
parent 04041bb54a
commit 9770138eee

View File

@@ -5,7 +5,7 @@
* *
* Matches selector strings to pages * Matches selector strings to pages
* *
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer * ProcessWire 3.x, Copyright 2024 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* Hookable methods: * Hookable methods:
@@ -1817,6 +1817,7 @@ class PageFinder extends Wire {
$tableAlias = $database->escapeTable($tableAlias); $tableAlias = $database->escapeTable($tableAlias);
$join = ''; $join = '';
$joinType = '';
$numEmptyValues = 0; $numEmptyValues = 0;
$valueArray = $selector->values(true); $valueArray = $selector->values(true);
$fieldtype = $field->type; $fieldtype = $field->type;
@@ -1846,6 +1847,7 @@ class PageFinder extends Wire {
$q = $subqueries[$tableAlias]; $q = $subqueries[$tableAlias];
} else { } else {
$q = $this->wire(new DatabaseQuerySelect()); $q = $this->wire(new DatabaseQuerySelect());
// $subqueries[$tableAlias] = $q;
} }
/** @var PageFinderDatabaseQuerySelect $q */ /** @var PageFinderDatabaseQuerySelect $q */
@@ -1855,12 +1857,17 @@ class PageFinder extends Wire {
$q->set('selectors', $selectors); // original selectors (all) if required by the fieldtype $q->set('selectors', $selectors); // original selectors (all) if required by the fieldtype
$q->set('parentQuery', $query); $q->set('parentQuery', $query);
$q->set('pageFinder', $this); $q->set('pageFinder', $this);
$q->set('joinType', $joinType);
$q->bindOption('global', true); // ensures bound value key are globally unique $q->bindOption('global', true); // ensures bound value key are globally unique
$q->bindOption('prefix', 'pf'); // pf=PageFinder $q->bindOption('prefix', 'pf'); // pf=PageFinder
$q = $fieldtype->getMatchQuery($q, $tableAlias, $subfield, $selector->operator, $value); $q = $fieldtype->getMatchQuery($q, $tableAlias, $subfield, $selector->operator, $value);
$q->copyTo($query, array('select', 'join', 'leftjoin', 'orderby', 'groupby')); $q->copyTo($query, array('select', 'join', 'leftjoin', 'orderby', 'groupby'));
$q->copyBindValuesTo($query); $q->copyBindValuesTo($query);
if($q->joinType && $q->joinType != $joinType) {
$joinType = strtolower((string) $q->joinType);
}
if(count($q->where)) { if(count($q->where)) {
// $and = $selector->not ? "AND NOT" : "AND"; // $and = $selector->not ? "AND NOT" : "AND";
@@ -1883,9 +1890,8 @@ class PageFinder extends Wire {
} }
if($join) { if($join) {
$joinType = 'join'; if($joinType === 'leftjoin'
|| count($fields) > 1
if(count($fields) > 1
|| !empty($options['startAfterID']) || !empty($options['stopBeforeID']) || !empty($options['startAfterID']) || !empty($options['stopBeforeID'])
|| (count($valueArray) > 1 && $numEmptyValues > 0) || (count($valueArray) > 1 && $numEmptyValues > 0)
|| ($subfield == 'count' && !$this->isRepeaterFieldtype($field->type)) || ($subfield == 'count' && !$this->isRepeaterFieldtype($field->type))
@@ -1893,7 +1899,7 @@ class PageFinder extends Wire {
|| $selector->operator == '!=') { || $selector->operator == '!=') {
// join should instead be a leftjoin // join should instead be a leftjoin
$joinType = "leftjoin"; $joinType = 'leftjoin';
if($where) { if($where) {
$whereType = $lastSelector->str == $selector->str ? "OR" : ") AND ("; $whereType = $lastSelector->str == $selector->str ? "OR" : ") AND (";
@@ -1905,6 +1911,8 @@ class PageFinder extends Wire {
// removes condition from join, but ensures we still have a $join // removes condition from join, but ensures we still have a $join
$join = '1=1'; $join = '1=1';
} }
} else {
$joinType = 'join';
} }
// we compile the joins after going through all the selectors, so that we can // we compile the joins after going through all the selectors, so that we can
@@ -3743,5 +3751,6 @@ class PageFinder extends Wire {
* @property Selectors $selectors Original Selectors object * @property Selectors $selectors Original Selectors object
* @property DatabaseQuerySelect $parentQuery Parent database query * @property DatabaseQuerySelect $parentQuery Parent database query
* @property PageFinder $pageFinder PageFinder instance that initiated the query * @property PageFinder $pageFinder PageFinder instance that initiated the query
* @property string $joinType Value 'join', 'leftjoin', or '' (if not yet known), can be overridden (3.0.237+)
*/ */
abstract class PageFinderDatabaseQuerySelect extends DatabaseQuerySelect { } abstract class PageFinderDatabaseQuerySelect extends DatabaseQuerySelect { }