Skip to content

Commit 1a2ef27

Browse files
perf(ForeignModelField): adjust foreign model indexing
1 parent c977141 commit 1a2ef27

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Fields/ForeignModelField.inc

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,20 @@ class ForeignModelField extends Field {
341341
}
342342

343343
/**
344-
* Indexes all in scope Model objects by their $model_field values for quick lookup and returns
345-
* an associative array of the indexed Model objects. This method will also cache the indices
344+
* Indexes all in scope Model objects by their $model_field and $model_field_internal values for quick lookup and
345+
* returns an associative array of the indexed Model objects. This method will also cache the indices
346346
* in the static $model_index property to prevent redundant indexing operations during the same request.
347-
* @return array An associative array mapping $model_field values to their corresponding Model objects.
347+
*
348+
* Index is structured as:
349+
* [
350+
* 'ModelClassShortName' => [
351+
* 'this_field_name' => [
352+
* 'model_field_value' => ModelObject,
353+
* 'model_field_internal_value' => ModelObject,
354+
* ],
355+
*
356+
* @return array An associative array mapping $model_field and $model_field_internal values to their corresponding
357+
* Model objects.
348358
*/
349359
public function get_model_index(): array {
350360
# Get the name of the Model this Field belongs to
@@ -356,34 +366,37 @@ class ForeignModelField extends Field {
356366
}
357367

358368
# Loop through each in scope Model object and index them by their $model_field values
359-
foreach($this->get_in_scope_models()->model_objects as $model_object) {
360-
$foreign_model_field_value = $model_object->{$this->model_field_internal}->value;
361-
self::$model_index[$model_context_name][$this->name][$foreign_model_field_value] = $model_object;
369+
foreach ($this->get_in_scope_models()->model_objects as $model_object) {
370+
$foreign_model_field_value = $model_object->{$this->model_field}->value;
371+
$foreign_model_field_internal_value = $model_object->{$this->model_field_internal}->value;
372+
self::$model_index[$model_context_name][$this->name][$this->model_field][
373+
$foreign_model_field_value
374+
] = $model_object;
375+
self::$model_index[$model_context_name][$this->name][$this->model_field_internal][
376+
$foreign_model_field_internal_value
377+
] = $model_object;
362378
}
363379

364380
# Return the indexed Model objects for this Field
365-
return self::$model_index[$model_context_name][$this->name];
381+
return self::$model_index[$model_context_name][$this->name] ?? [];
366382
}
367383

368384
/**
369385
* Clears the cached model indices.
370386
*/
371-
public static function clear_model_index(): void
372-
{
387+
public static function clear_model_index(): void {
373388
self::$model_index = [];
374389
}
375390

376391
/**
377392
* Obtains a ModelSet of the Model(s) that match this field's criteria.
378-
* @param string $field_name The name of the field used to check for matching values. This is typically set to the
379-
* same value as $this->field_name.
380393
* @param mixed $field_value The value of the $field_name that indicates there is a match. This is typically set
381394
* to the same value as $this->value.
382395
*/
383396
private function __get_matches(string $field_name, mixed $field_value): ModelSet {
384397
# Create a ModelSet we can use to store matching objects
385398
$modelset = new ModelSet();
386-
$match = $this->get_model_index()[$field_value] ?? null;
399+
$match = $this->get_model_index()[$field_name][$field_value] ?? null;
387400

388401
# Only add the Model object if it exists
389402
if ($match) {
@@ -399,6 +412,11 @@ class ForeignModelField extends Field {
399412
* @returns Model|null Returns the Model object associated with this Field's current value.
400413
*/
401414
public function get_related_model(): Model|null {
415+
# Skip for non-many fields
416+
if ($this->many) {
417+
return null;
418+
}
419+
402420
# Get the Model objects that match this field's criteria
403421
$query_modelset = $this->__get_matches($this->model_field, $this->value);
404422

0 commit comments

Comments
 (0)