Skip to content

Added fetch() to DF_Model

Robert Sinton requested to merge feature/df_model__fetch into master

This is a method I have been using a lot in recent work, by defining it in each model where I use it.

Basically it is an 'unforgiving' version of get(), which will throw an exception in any case other than there being exactly one record with the specified id. It is for use in any circumstance where we have good reason to believe a single record exists with that primary key value.

It is very useful in a sequence of commands where it can be safely relied on — as opposed to get() — to either deliver a result that is useable for the next step, or else to throw an exception if not.

This allows for much cleaner and clearer 'happy path' coding, were we use the Exception mechanism to deal with unexpected outcomes rather than having to check the outcome of get() (or risk not checking it at all!)

What I did

  • Added fetch() method.

Implications

None

Setup

None

How to test

  • Code review.
  • Test with suitable code
    • Example: commit 1fbd75147c5b28c427e84de735318e38256ffc6d in Pulse, which uses it on line 127 in api/v1/suppliers.php when loading a supplier record to update.
      • Make sure to remove or disable the definition of fetch() in dandi_suppliers_model first.
Edited by Robert Sinton

Merge request reports