Skip to content

Commit 2ae5bbc

Browse files
committed
New: Support for including database columns other than the label and value in Options reponse
1 parent c80526e commit 2ae5bbc

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

Editor/Options.php

+50-12
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ class Options extends Ext
7171
/** @var bool Indicate if options should always be refreshed */
7272
private $_alwaysRefresh = true;
7373

74-
/** @var string Table to get the information from */
75-
private $_table;
74+
/** @var callable|null */
75+
private $_customFn;
7676

77-
/** @var string Column name containing the value */
78-
private $_value;
77+
/** @var string[] List of columns to include in the output */
78+
private $_includes = [];
7979

8080
/** @var string[] Column names for the label(s) */
8181
private $_label = [];
@@ -86,22 +86,26 @@ class Options extends Ext
8686
/** @var int|null Row limit */
8787
private $_limit;
8888

89+
/** @var array List of label / values to use if not database retrieved */
90+
private $_manualAdd = [];
91+
92+
/** @var string|bool ORDER BY clause */
93+
private $_order = true;
94+
8995
/** @var callable Callback function to do rendering of labels */
9096
private $_renderer;
9197

9298
/** @var bool Indicate if options should get got for create/edit or on search only */
9399
private $_searchOnly = false;
94100

101+
/** @var string Table to get the information from */
102+
private $_table;
103+
95104
/** @var callable Callback function to add where conditions */
96105
private $_where;
97106

98-
/** @var string|bool ORDER BY clause */
99-
private $_order = true;
100-
101-
private $_manualAdd = [];
102-
103-
/** @var callable|null */
104-
private $_customFn;
107+
/** @var string Column name containing the value */
108+
private $_value;
105109

106110
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
107111
* Public methods
@@ -156,6 +160,29 @@ public function fn($_ = null)
156160
return $this->_getSet($this->_customFn, $_);
157161
}
158162

163+
/**
164+
* Column names from `value()` and `label()` that should be included in the output object for
165+
* each option, in addition to the value and label.
166+
*
167+
* @param string|string[] $inc The list of columns to include in the output
168+
*
169+
* @return ($_ is null ? string[] : $this)
170+
*/
171+
public function include($inc = null)
172+
{
173+
if ($inc === null) {
174+
return $this->_includes;
175+
}
176+
177+
if (is_array($inc)) {
178+
$this->_includes = array_merge($this->_includes, $inc);
179+
} else {
180+
$this->_includes[] = $inc;
181+
}
182+
183+
return $this;
184+
}
185+
159186
/**
160187
* Get / set the column(s) to use as the label value of the options.
161188
*
@@ -392,10 +419,21 @@ public function exec($db, $refresh, $search = null)
392419
// Apply the search to the rendered label. Need to do it here rather than in SQL since
393420
// the label is rendered in PHP.
394421
if ($search === null || $search === '' || stripos($rowLabel, $search) === 0) {
395-
$out[] = [
422+
$option = [
396423
'label' => $rowLabel,
397424
'value' => $rowValue,
398425
];
426+
427+
// Add in any columns that are needed for extra data (includes)
428+
for ($j = 0; $j < count($this->_includes); ++$j) {
429+
$inc = $this->_includes[$j];
430+
431+
if (isset($rows[$i][$inc])) {
432+
$option[$inc] = $rows[$i][$inc];
433+
}
434+
}
435+
436+
$out[] = $option;
399437
}
400438

401439
// Limit needs to be done in PHP to allow for the PHP based filtering above, and also

0 commit comments

Comments
 (0)