[Catalyst] CDBI, Mason, and null foreign keys

Matt S Trout catalyst at trout.me.uk
Thu Jul 21 00:21:16 CEST 2005

On Wed, Jul 20, 2005 at 02:51:14PM -0700, Alan Humphrey wrote:
> I'm using CDBI (model) and Mason (view) for my application.  I like both but
> I'm finding myself doing this a lot in my mason templates:
> % print $c->stash->{source}->get('url_id')->get('url') if defined
> $c->stash->{source}->get('url_id');

Hmm, in TT you can just do [% obj.get('url_id').get('url') %] and if the
first get returns null TT won't try the method call on the second.

Why not $c->stash->{source}->url_id->url btw? That would cut down on at
least some of your clutter. 
> As an experiment I tried "flattening" the database results in the controller
> so all the template sees is an array of hashes.  That worked as well and now
> the clutter is in the controller.
That's ... kinda ugly though.  
> Any opinions as to the better approach?  Any other approaches?  Is there a
> better way to handle null foreign key references in CDBI?

Either have the key as zero instead of NULL and a special empty object
with an id of zero?

eval { $c->{stash}{source}->url_id->url }

is a bit shorter, though obviously a bit evil.

     Matt S Trout           Website: http://www.shadowcatsystems.co.uk
  Technical Director        E-mail:  mst (at) shadowcatsystems.co.uk
Shadowcat Systems Ltd.

More information about the Catalyst mailing list