@@ -16,25 +16,8 @@ use RESTAPI\Responses\ServerError;
1616 * For example, a ForeignModelField can be used to relate a static route to its parent Gateway model object.
1717 */
1818class ForeignModelField extends Field {
19- /**
20- * @var string $MODELS_NAMESPACE The namespace prefix for all Model classes.
21- */
2219 const MODELS_NAMESPACE = 'RESTAPI \\Models \\' ;
23-
24- /**
25- * @var array $models An array of instantiated Model objects for each assigned $model_name.
26- */
2720 public array $ models = [];
28-
29- /**
30- * @var array $model_index
31- * An associative array that maps objects of the assigned $model_name classes by their $model_field values
32- * for quick lookup. This ensures the most taxing reference operations are only performed once per request.
33- * Indices for each corresponding $model_name will be cleared automatically when any Model object of that class
34- * is created, updated, or deleted.
35- */
36- public static array $ model_index = [];
37-
3821 /**
3922 * Defines the ForeignModelField object and sets its options.
4023 * @param string|array $model_name The name(s) of the foreign Model class(es) this field relates to. This should be
@@ -194,7 +177,7 @@ class ForeignModelField extends Field {
194177 if (!class_exists ($ model_name )) {
195178 throw new ServerError (
196179 message: "ForeignModelField's `model_name` property must be an existing Model class FQN, " .
197- "received ` $ model_name`. " ,
180+ "received ` $ model_name`. " ,
198181 response_id: 'FOREIGN_MODEL_FIELD_WITH_UNKNOWN_MODEL_NAME ' ,
199182 );
200183 }
@@ -295,7 +278,7 @@ class ForeignModelField extends Field {
295278 if (!$ query_modelset ->exists ()) {
296279 throw new NotFoundError (
297280 message: "Field ` $ this ->name ` could not locate ` $ model_names` object with " .
298- "` $ this ->model_field ` set to ` $ value` " ,
281+ "` $ this ->model_field ` set to ` $ value` " ,
299282 response_id: 'FOREIGN_MODEL_FIELD_VALUE_NOT_FOUND ' ,
300283 );
301284 }
@@ -340,70 +323,15 @@ class ForeignModelField extends Field {
340323 return $ in_scope_models ->query ($ this ->model_query );
341324 }
342325
343- /**
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
346- * in the static $model_index property to prevent redundant indexing operations during the same request.
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.
358- */
359- public function get_model_index (): array {
360- # Get the name of the Model this Field belongs to
361- $ model_context_name = $ this ->context ->get_class_shortname ();
362-
363- # Check if we have already indexed Model objects for this Field in this request
364- if (self ::$ model_index [$ model_context_name ][$ this ->name ]) {
365- return self ::$ model_index [$ model_context_name ][$ this ->name ];
366- }
367-
368- # Loop through each in scope Model object and index them by their $model_field values
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 ;
378- }
379-
380- # Return the indexed Model objects for this Field
381- return self ::$ model_index [$ model_context_name ][$ this ->name ] ?? [];
382- }
383-
384- /**
385- * Clears the cached model indices.
386- */
387- public static function clear_model_index (): void {
388- self ::$ model_index = [];
389- }
390-
391326 /**
392327 * Obtains a ModelSet of the Model(s) that match this field's criteria.
328+ * @param string $field_name The name of the field used to check for matching values. This is typically set to the
329+ * same value as $this->field_name.
393330 * @param mixed $field_value The value of the $field_name that indicates there is a match. This is typically set
394331 * to the same value as $this->value.
395332 */
396333 private function __get_matches (string $ field_name , mixed $ field_value ): ModelSet {
397- # Create a ModelSet we can use to store matching objects
398- $ modelset = new ModelSet ();
399- $ match = $ this ->get_model_index ()[$ field_name ][$ field_value ] ?? null ;
400-
401- # Only add the Model object if it exists
402- if ($ match ) {
403- $ modelset ->model_objects [] = $ match ;
404- }
405-
406- return $ modelset ;
334+ return $ this ->get_in_scope_models ()->query (query_params: [$ field_name => $ field_value ]);
407335 }
408336
409337 /**
@@ -412,11 +340,6 @@ class ForeignModelField extends Field {
412340 * @returns Model|null Returns the Model object associated with this Field's current value.
413341 */
414342 public function get_related_model (): Model |null {
415- # Skip for non-many fields
416- if ($ this ->many ) {
417- return null ;
418- }
419-
420343 # Get the Model objects that match this field's criteria
421344 $ query_modelset = $ this ->__get_matches ($ this ->model_field , $ this ->value );
422345
@@ -451,4 +374,4 @@ class ForeignModelField extends Field {
451374
452375 return $ modelset ;
453376 }
454- }
377+ }
0 commit comments