1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 17:24:46 +02:00

Add support to accept array value for Fieldtype::getMatchQuery(), plus documentation improvements to that method

This commit is contained in:
Ryan Cramer
2021-07-23 09:51:42 -04:00
parent 916ac6f5c5
commit 119ee9ab8c

View File

@@ -700,30 +700,56 @@ abstract class Fieldtype extends WireData implements Module {
/**
* Get the database query that matches a Fieldtype tables data with a given value.
*
* Possible template method: If overridden, children should not call this parent method.
* Possible template method: If overridden, children do not need to call this method
* if they update the $query themselves.
*
* Note the following additional properties are available from the $query argument:
*
* - `$query->field` (Field): Field instance that being referred to match.
* - `$query->group` (string): Original group of the field in the selector (when applicable).
* - `$query->selector` (Selector): Original Selector object (matching the $field).
* - `$query->selectors` (Selectors): Original Selectors object (matching $field and others).
* - `$query->parentQuery` (DatabaseQuerySelect): Parent database query that $query will be merged into.
* - `$query->pageFinder` (PageFinder): The PageFinder instance that initiated the query, for additional info.
*
* #pw-group-finding
*
* @param PageFinderDatabaseQuerySelect $query
* @param string $table The table name to use
* @param string $subfield Name of the subfield (typically 'data', unless selector explicitly specified another)
* @param string $operator The comparison operator
* @param mixed $value The value to find
* @param string $operator The comparison operator.
* - This base Fieldtype class accepts only database operators (=, !=, >, >=, <, <=, &).
* - Other Fieldtypes may choose to accept more operators according to need of Fieldtype.
* @param mixed $value Value to find.
* - If given array, this base Fieldtype class (only) will match via OR condition. (3.0.182+)
* - Other Fieldtypes may choose to interpret array values differently according need of Fieldtype.
* @return PageFinderDatabaseQuerySelect|DatabaseQuerySelect $query
* @throws WireException
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
$database = $this->wire('database');
$database = $this->wire()->database;
if(!$database->isOperator($operator))
throw new WireException("Operator '{$operator}' is not implemented in {$this->className}");
if(!$database->isOperator($operator)) {
throw new WireException("Operator '{$operator}' is not implemented in {$this->className}");
}
$table = $database->escapeTable($table);
$subfield = $database->escapeCol($subfield);
$operator = $database->escapeOperator($operator, WireDatabasePDO::operatorTypeComparison);
$query->where("{$table}.{$subfield}{$operator}?", $value); // QA
if(is_array($value)) {
$a = array();
foreach($value as $v) {
$bindKey = $query->bindValueGetKey($v);
$a[] = "{$table}.{$subfield}{$operator}{$bindKey}";
}
$query->where('(' . implode(' OR ', $a) . ')');
} else {
$query->where("{$table}.{$subfield}{$operator}?", $value); // QA
}
return $query;
}