[Catalyst] Model/Controller logic separation - best practices?

Juan Miguel Paredes juan.paredes at gmail.com
Tue Mar 4 21:13:01 GMT 2008


On Wed, Mar 5, 2008 at 3:05 PM, Ian Sillitoe <catalyst at sillit.com> wrote:
>
> Controller/TableA.pm:
> $c->{stash}->{row_a} = $c->model('MyDBIC::TableA')->find('id');
> $c->{stash}->{template} = 'view_a.tt2';
> $c->forward('Catalyst::View::TT');
>
> /root/src/view_a.tt2:
> [% rows_b = row_a.get_related_rows( optional => param ) %]
> [% FOREACH row_b = rows_b %]
>   [% DisplayRow(row_b) %]
> [% END %]
>

Perhaps there should be no need for the custom get_related_rows call
(and doing in the template should be, in fact, breaking MVC
principles, IMHO).  If your DBIC Classes representing TableA and
TableB are properly related (master-detail, in this case, for what
shows from the example, something like: TableA has_many TableB) with
the method, say, gimme_b, something like the following should work:

/root/src/view_a.tt2:
[% rows_b = row_a.gimme_b %]
[% FOREACH row_b = rows_b %]
  [% DisplayRow(row_b) %]
[% END %]

If, for some odd reason, the DBIC Relationships (a definite _must see_
on the docs) are not enough for your needs, you could put all your
related rows from TableB on the stash (calling the custom model method
from your controller), so you don't make direct calls from your
template to your models.

Anyway, that's just an opinion. HTH.



More information about the Catalyst mailing list