Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

CDBI, Mason, and null foreign keys

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


alan.humphreyatcomcast.net

Jul 20, 2005, 11:49 PM

Post #1 of 2 (122 views)
Permalink
CDBI, Mason, and null foreign keys

All -



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');



In other words, check that the object exists before trying to reference it.
In this example the 'sources' table has an optional column 'url_id' which
holds an id to the 'urls' table. Using that id I want to return the actual
url.



This works just fine, but it seems a little verbose.



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.



Any opinions as to the better approach? Any other approaches? Is there a
better way to handle null foreign key references in CDBI?



- Alan



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/catalyst/attachments/20050720/5181baf2/attachment.htm


catalystattrout.me.uk

Jul 21, 2005, 12:11 AM

Post #2 of 2 (119 views)
Permalink
CDBI, Mason, and null foreign keys [In reply to]

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.

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.