Re: Mapping column values to objects on create() or select().

[prev] [thread] [next] [Date index for 2005/06/23]

From: Stepan Riha
Subject: Re: Mapping column values to objects on create() or select().
Date: 15:33 on 23 Jun 2005
Quoting rcthomas3000+cdbi@xxxxx.xxx:
> Often, I will have a table where several columns are foreign keys to
> some other table.  The other table has a primary key that's not useful
> for a (normal) humans -- it's an integer.  But there's another column
> in the referenced table that's a unique string.  So it's annoying for
> me to have to do
>
>  my $film = Film->retrieve( name => 'Clash of the Titans' );
>  my $theater = Theater->retrieve( name => 'Cinemegaloplex III' );
>  my $showing =
>     Show->create( { film => $film, theater => $theater, time => ... } );

As someone else said, you can create a custom create method that takes 
the film
and theater names.

However, I wonder where you are getting these names from?  Is it a free-form
text entry the user types them in (and if so, what happens if they 
enter a film
or theater name that doesn't exist)?  Or do they select it from a list of
filems/theaters?  If it's the later, why doesn't the list return the primary
key of the film/theater?  That would seem the more logical and 
efficient way...

> I really
> want the same thing to handle my selects, too.  Say, return all the
> showings of my movie by just searching on the string, not retrieving 
> it first.

Again, if the user uses lists to specify the film/theater, these could simply
return the primary key of the selected item, rather than the text shown to the
user.

If they are typing in the film or theater name, you may want to use
CDBI::Plugin::DeepAbstractSearch.  You'll be than able to do something like
this:

my @showings = Show->deep_search_where(
     { 'film.name' => { -like => '%$film_name%' } } );

     - Stepan

Re: Mapping column values to objects on create() or select().
Stepan Riha 15:33 on 23 Jun 2005

Generated at 16:36 on 28 Jul 2005 by mariachi v0.52