[Solar-talk] Changes afoot: a new Model class

Rodrigo Moraes rodrigo.moraes at gmail.com
Wed Feb 7 20:15:48 PST 2007


On 2/8/07, Rodrigo Moraes wrote:
> Well, the idea I had was to add a third optional parameter to
> fetchRelated(), with $options to override the defaults defined in the
> relationship.
>
>          // get the options for this relationship
>         $opts = array_merge($this->_related[$name], (array) $options);
>
> Then the options could have definitions for the limit (count/offset).

Well, it works, and for me it seems clean. :)
Here's what I did (I removed some checkings):

class Tipos_Sql_Model_Record extends Solar_Sql_Model_Record
{
    public function fetchRelated($name, $paging = null, $page = null)
    {
        $primary_key = $this->_model->col_primary;

        $options = array(
            'limit'  => $paging,
            'offset' => ((int) $paging * (int) $page) - (int) $paging,
        );

        $this->_data[$name] = $this->_model->fetchRelated(
            $name,
            $this->_data[$primary_key],
            $options
        );

        return $this->$name;
    }
}

Then in Solar_Sql_Model I have:

    public function fetchRelated($name, $spec, $options = null)
    {
        [snip]

        // get the options for this relationship
        if($options) {
            $opts = array_merge($this->_related[$name], (array) $options);
        } else {
            $opts = $this->_related[$name];
        }

        [snip]

        $select->limit($opts['limit'], $opts['offset']);

...

I also see the third parameter $options as a nice addition for
subclasses (plus that the relationship could be override at run time).
Perhaps not?

rodrigo


More information about the solar-talk mailing list