mirror of
https://github.com/processwire/processwire.git
synced 2025-08-11 17:24:46 +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:
@@ -516,6 +516,10 @@ class Pages extends Wire {
|
|||||||
* // especially useful with fields like Table or Combo that might have several
|
* // especially useful with fields like Table or Combo that might have several
|
||||||
* // different columns:
|
* // different columns:
|
||||||
* $a = $pages->findRaw("template=villa", "rates_table.*" );
|
* $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
|
* #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|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='')
|
* @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
|
* @param array $options
|
||||||
* @return array
|
* @return array
|
||||||
* @since 3.0.172
|
* @since 3.0.172
|
||||||
|
@@ -110,6 +110,18 @@ class PagesRawFinder extends Wire {
|
|||||||
*/
|
*/
|
||||||
protected $nativeFields = array();
|
protected $nativeFields = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected $parentFields = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected $childrenFields = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
@@ -193,6 +205,34 @@ class PagesRawFinder extends Wire {
|
|||||||
$this->getAll = false;
|
$this->getAll = false;
|
||||||
$this->ids = null;
|
$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)) {
|
if(empty($field)) {
|
||||||
$this->getAll = true;
|
$this->getAll = true;
|
||||||
|
|
||||||
@@ -277,30 +317,37 @@ class PagesRawFinder extends Wire {
|
|||||||
protected function splitFields() {
|
protected function splitFields() {
|
||||||
|
|
||||||
$fields = $this->wire()->fields;
|
$fields = $this->wire()->fields;
|
||||||
|
$fails = array();
|
||||||
|
|
||||||
// split request fields into custom fields and native (pages table) fields
|
// split request fields into custom fields and native (pages table) fields
|
||||||
foreach($this->requestFields as $key => $fieldName) {
|
foreach($this->requestFields as $key => $fieldName) {
|
||||||
|
|
||||||
if(empty($fieldName)) continue;
|
if(empty($fieldName)) continue;
|
||||||
if(is_string($fieldName)) $fieldName = trim($fieldName);
|
if(is_string($fieldName)) $fieldName = trim($fieldName);
|
||||||
|
|
||||||
$colName = '';
|
$colName = '';
|
||||||
$dotCol = false;
|
$dotCol = false;
|
||||||
|
$fieldObject = null;
|
||||||
|
$fullName = $fieldName;
|
||||||
|
|
||||||
if($fieldName === '*') {
|
if($fieldName === '*') {
|
||||||
// get all (not yet supported)
|
// get all (not yet supported)
|
||||||
$fieldObject = null;
|
|
||||||
|
|
||||||
} else if($fieldName instanceof Field) {
|
} else if($fieldName instanceof Field) {
|
||||||
// Field object
|
// Field object
|
||||||
$fieldObject = $fieldName;
|
$fieldObject = $fieldName;
|
||||||
|
|
||||||
} else if(is_array($fieldName)) {
|
} else if(is_array($fieldName)) {
|
||||||
// Array where [ 'field' => [ 'subfield'' ]]
|
// Array where [ 'field' => [ 'subfield' ]]
|
||||||
$colName = $fieldName; // array
|
$colName = $fieldName; // array
|
||||||
$fieldName = $key;
|
$fieldName = $key;
|
||||||
|
if($fieldName === 'parent' || $fieldName === 'children') {
|
||||||
|
// passthru
|
||||||
|
} else {
|
||||||
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
|
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
|
||||||
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
|
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
|
||||||
if(!$fieldObject) continue;
|
if(!$fieldObject) continue;
|
||||||
|
}
|
||||||
|
|
||||||
} else if(is_int($fieldName) || ctype_digit("$fieldName")) {
|
} else if(is_int($fieldName) || ctype_digit("$fieldName")) {
|
||||||
// Field ID
|
// Field ID
|
||||||
@@ -315,15 +362,28 @@ class PagesRawFinder extends Wire {
|
|||||||
list($fieldName, $colName) = explode('[', $fieldName, 2);
|
list($fieldName, $colName) = explode('[', $fieldName, 2);
|
||||||
$colName = rtrim($colName, ']');
|
$colName = rtrim($colName, ']');
|
||||||
}
|
}
|
||||||
|
if($fieldName === 'parent' || $fieldName === 'children') {
|
||||||
|
// passthru
|
||||||
|
} else {
|
||||||
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
|
$fieldObject = isset($this->customFields[$fieldName]) ? $this->customFields[$fieldName] : null;
|
||||||
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
|
if(!$fieldObject) $fieldObject = $fields->get($fieldName);
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// something we do not recognize
|
// something we do not recognize
|
||||||
|
$fails[] = $fieldName;
|
||||||
continue;
|
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;
|
$this->customFields[$fieldName] = $fieldObject;
|
||||||
if(!empty($colName)) {
|
if(!empty($colName)) {
|
||||||
$colNames = is_array($colName) ? $colName : array($colName);
|
$colNames = is_array($colName) ? $colName : array($colName);
|
||||||
@@ -340,6 +400,8 @@ class PagesRawFinder extends Wire {
|
|||||||
$this->nativeFields[$fieldName] = $fieldName;
|
$this->nativeFields[$fieldName] = $fieldName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(count($fails)) $this->unknownFieldsException($fails);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -350,6 +412,7 @@ class PagesRawFinder extends Wire {
|
|||||||
|
|
||||||
$this->ids = array();
|
$this->ids = array();
|
||||||
$allNatives = array();
|
$allNatives = array();
|
||||||
|
$fails = array();
|
||||||
|
|
||||||
foreach($this->findIDs($this->selector, '*') as $row) {
|
foreach($this->findIDs($this->selector, '*') as $row) {
|
||||||
$id = (int) $row['id'];
|
$id = (int) $row['id'];
|
||||||
@@ -388,9 +451,12 @@ class PagesRawFinder extends Wire {
|
|||||||
$getNatives[$colName] = $colName;
|
$getNatives[$colName] = $colName;
|
||||||
} else {
|
} else {
|
||||||
// fieldName is not a known field or pages column
|
// fieldName is not a known field or pages column
|
||||||
|
$fails[] = "$fieldName";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(count($fails)) $this->unknownFieldsException($fails, 'column/field');
|
||||||
|
|
||||||
if(count($getNatives)) {
|
if(count($getNatives)) {
|
||||||
// remove any native data that is present but was not requested
|
// remove any native data that is present but was not requested
|
||||||
foreach($this->values as $id => $row) {
|
foreach($this->values as $id => $row) {
|
||||||
@@ -731,4 +797,10 @@ class PagesRawFinder extends Wire {
|
|||||||
}
|
}
|
||||||
return $this->ids;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user