1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 09:14:58 +02:00

Add support for specifying "field=foo|bar|baz" in selector to $pages->findRaw(), like you can with $pages->find(). This is an alternative to using the separate field argument. Also begin support for adding parent properties, but there is more work to be done there before they are supported.

This commit is contained in:
Ryan Cramer
2021-02-12 15:29:29 -05:00
parent ae46639e35
commit 199859b6ff
2 changed files with 86 additions and 9 deletions

View File

@@ -516,6 +516,10 @@ class Pages extends Wire {
* // especially useful with fields like Table or Combo that might have several
* // different columns:
* $a = $pages->findRaw("template=villa", "rates_table.*" );
*
* // If you prefer, you can specify the field name(s) in the selector (3.0.173+):
* $a = $pages->findRaw("template=blog, field=title");
* $a = $pages->findRaw("template=blog, field=title|categories.title");
* ~~~~~
*
* #pw-advanced
@@ -523,6 +527,7 @@ class Pages extends Wire {
*
* @param string|array|Selectors|int $selector Page matching selector or page ID
* @param string|array|Field $field Name of field/property to get, or array of them, or omit to get all (default='')
* Note: this argument may also be specified in the $selector argument as "field=foo" or "field=foo|bar|baz" (3.0.173+).
* @param array $options
* @return array
* @since 3.0.172

View File

@@ -109,6 +109,18 @@ class PagesRawFinder extends Wire {
*
*/
protected $nativeFields = array();
/**
* @var array
*
*/
protected $parentFields = array();
/**
* @var array
*
*/
protected $childrenFields = array();
/**
* @var array
@@ -193,6 +205,34 @@ class PagesRawFinder extends Wire {
$this->getAll = false;
$this->ids = null;
if(empty($field)) {
// check if field specified in selector instead
$field = array();
$multi = false;
$fields = $this->wire()->fields;
if(!$selector instanceof Selectors) {
$selector = new Selectors($selector);
$this->wire($selector);
}
foreach($selector as $item) {
$name = $item->field();
if($name !== 'field' && $name !== 'join' && $name !== 'fields') continue;
if($name !== 'fields' && $fields->get($name)) continue;
$value = $item->value;
if(is_array($value)) {
$field = array_merge($field, $value);
} else if($value === 'all') {
$this->getAll = true;
} else {
$field[] = $value;
}
$selector->remove($item);
if($name === 'fields') $multi = true;
}
$this->selector = $selector;
if(!$multi && count($field) === 1) $field = reset($field);
}
if(empty($field)) {
$this->getAll = true;
@@ -277,30 +317,37 @@ class PagesRawFinder extends Wire {
protected function splitFields() {
$fields = $this->wire()->fields;
$fails = array();
// split request fields into custom fields and native (pages table) fields
foreach($this->requestFields as $key => $fieldName) {
if(empty($fieldName)) continue;
if(is_string($fieldName)) $fieldName = trim($fieldName);
$colName = '';
$dotCol = false;
$fieldObject = null;
$fullName = $fieldName;
if($fieldName === '*') {
// get all (not yet supported)
$fieldObject = null;
} else if($fieldName instanceof Field) {
// Field object
$fieldObject = $fieldName;
} else if(is_array($fieldName)) {
// Array where [ 'field' => [ 'subfield'' ]]
// Array where [ 'field' => [ 'subfield' ]]
$colName = $fieldName; // array
$fieldName = $key;
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
if(!$fieldObject) continue;
if($fieldName === 'parent' || $fieldName === 'children') {
// passthru
} else {
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
if(!$fieldObject) continue;
}
} else if(is_int($fieldName) || ctype_digit("$fieldName")) {
// Field ID
@@ -315,15 +362,28 @@ class PagesRawFinder extends Wire {
list($fieldName, $colName) = explode('[', $fieldName, 2);
$colName = rtrim($colName, ']');
}
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
if($fieldName === 'parent' || $fieldName === 'children') {
// passthru
} else {
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
}
} else {
// something we do not recognize
$fails[] = $fieldName;
continue;
}
if($fieldObject instanceof Field) {
if($fieldName === 'parent') {
// @todo not yet supported
$this->parentFields[$fullName] = array($fieldName, $colName);
} else if($fieldName === 'children') {
// @todo not yet supported
$this->childrenFields[$fullName] = array($fieldName, $colName);
} else if($fieldObject instanceof Field) {
$this->customFields[$fieldName] = $fieldObject;
if(!empty($colName)) {
$colNames = is_array($colName) ? $colName : array($colName);
@@ -340,6 +400,8 @@ class PagesRawFinder extends Wire {
$this->nativeFields[$fieldName] = $fieldName;
}
}
if(count($fails)) $this->unknownFieldsException($fails);
}
/**
@@ -350,6 +412,7 @@ class PagesRawFinder extends Wire {
$this->ids = array();
$allNatives = array();
$fails = array();
foreach($this->findIDs($this->selector, '*') as $row) {
$id = (int) $row['id'];
@@ -365,7 +428,7 @@ class PagesRawFinder extends Wire {
if(!count($this->values)) return;
if($this->getAll) $this->nativeFields = $allNatives;
// native columns we will populate into $values
$getNatives = array();
@@ -388,8 +451,11 @@ class PagesRawFinder extends Wire {
$getNatives[$colName] = $colName;
} else {
// fieldName is not a known field or pages column
$fails[] = "$fieldName";
}
}
if(count($fails)) $this->unknownFieldsException($fails, 'column/field');
if(count($getNatives)) {
// remove any native data that is present but was not requested
@@ -731,4 +797,10 @@ class PagesRawFinder extends Wire {
}
return $this->ids;
}
protected function unknownFieldsException(array $fieldNames, $context = '') {
if($context) $context = " $context";
$s = "Unknown$context name(s) for findRaw: " . implode(', ', $fieldNames);
throw new WireException($s);
}
}