Retrieving the Minimum Model Object Data

Having used the Istarel Workshop Application Framework for quite some time, you know that it has some nifty callbacks and hooks to make your life as a developer easier.

However, you may not know that when you are proactively using the model object classes, you can minimize the data retrieved from the database.

Minimal Instantiation

You already know that if you want to work with an object based on a database record, you can do something like this:

$owner = Owner::retrieveOwner($owner_id);

That handy code is nice and concise, but you are instantiating the complete object; that is, all the columns from the owner table are retrieved. That can be unnecessarily wasteful in some cases. Instead, you can specify the columns (properties) that actually matter.

Suppose you were only retrieving the owner record because you needed her name? You could write this instead:

$owner = Owner::retrieveOwner($owner_id, 'owner_id', 'first_name, last_name');

Minimal Lookup of the "One" Object

That same feature exists in automatically-generated methods that retrieve the "one" object in a many-to-one relationship.

$specimens = Specimens::retrieveSpecimens();

foreach ($specimens as $specimen)
    $owner_email = $specimen->owner('email')->email;

Granted, that's probably not a fantastic use of this concept, but it illustrates the point: You still work with a bona fide Owner object in these examples, but you have limited what data you retrieved from the database.

Minimal Lookup of the "Many" Objects

This scenario is a bit more complicated, but only slightly so. For the automatically-generated methods that retrieve the "many" object in a many-to-one relationship, you pass an optional $requestor object along. In the normal circumstance where you pass a null or simply leave the argument to the method empty, you are essentially saying: "Do what you do by default: retrieve all the columns from the relevant objects."

class SpecimenGridViewer
    private $specimens;

    function initialize()
        $this->specimens = Specimens::retrieveSpecimens($this);

//  Callback from ORMFactory prior to object instantiation

    function restrictQuery($select)
        $select->setColumns('specimen_id, label, name, image_path');

In truth, restrictQuery($select) is but one of two optional callbacks in this circumstance. Because you might want a single object to act as the delegate for multiple such queries, there is a second callback made based on the model object class name of the objects being retrieved: restrictSpecimen($select).

In practice, I tend to always use the more specifically-named version to prevent later potential confusion.