[Solar-talk] Model helpers

Rodrigo Moraes rodrigo.moraes at gmail.com
Thu Sep 6 10:40:31 CDT 2007


Hey,
It's nothing new: today I thought that it could also be a good idea to
have model helpers (much like view, markdown, controller, filter and
image ones :), and so I implemented it in Tipos_Sql_Model. Oh noes! :)

Use case: I have some models that use the same logic to filter dates
and fetch within date ranges. I don't want to extend them from a
common class, neither to duplicate the logic. So I set a helper that
mainly filter and build the range parameters, and all models can use
it. One quick example:

    $model = Solar::factory('Tipos_Model_Something');

    // Set some ordinary parameters.
    $params = array('order' => 'created DESC');

    // Use the helper to add date range parameters.
    $date = ... requested date ...
    $valid = ... custom valid range ...
    $params = $model->helper('dateRangeParams', $date, $params, $valid);

    // Now fetch the records.
    $res = $model->fetchAll($params);

The example is very simple, but works well for the purposes of reusing
the logic - and, as in view helpers, in a clean way. I can think on
some other helpers that could be used to support common database
patterns and/or domain logic - shared pieces of model code, which
seems to be an appetizing idea.

Any thoughts?

-- rodrigo


More information about the Solar-talk mailing list