Skip to content

Commit 869934e

Browse files
fix(ForeignModelField): use index to find matches not queries
1 parent 6ed5ecf commit 869934e

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class ForeignModelField extends Field {
359359

360360
# Filter out Models that do not meet the assigned $model_query criteria
361361
$models = $models->query($this->model_query);
362-
$in_scope_modelsets[] = $models;
362+
$in_scope_modelsets[$model->get_class_fqn()] = $models;
363363
}
364364

365365
# Query for the Model object this value relates to.
@@ -374,16 +374,25 @@ class ForeignModelField extends Field {
374374
* to the same value as $this->value.
375375
*/
376376
private function __get_matches(string $field_name, mixed $field_value): ModelSet {
377+
self::_index_foreign_models();
378+
377379
# Loop through all in cope modelsets to find matches
378-
foreach ($this->get_in_scope_models() as $modelset) {
379-
# Query for Model objects that match this field's criteria
380-
$query_params = [$field_name => $field_value];
381-
$query_modelset = $modelset->query($query_params);
380+
foreach ($this->get_in_scope_models() as $model_class => $in_scope_modelset) {
381+
# Move to the next model class if we found no matches in the index
382+
if (!ModelCache::get_instance()::has_model($model_class, $field_name, $field_value)) {
383+
continue;
384+
}
382385

383-
# Return the matching ModelSet if it exists
384-
if ($query_modelset->exists()) {
385-
return $query_modelset;
386+
# Obtain the matched model
387+
$matched_model = ModelCache::get_instance()::fetch_model($model_class, $field_name, $field_value);
388+
389+
# Move to the next model class if the matched model we found was not in scope
390+
if ($this->model_query and !$in_scope_modelset->query(id: $matched_model->id)->exists()) {
391+
continue;
386392
}
393+
394+
# Otherwise, the matched model is valid. Return it within a ModelSet
395+
return new ModelSet(model_objects: [$matched_model]);
387396
}
388397

389398
# Return an empty ModelSet if no matches were found

0 commit comments

Comments
 (0)