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

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

From: Peter Speltz
Subject: Re: Mapping column values to objects on create() or select().
Date: 20:38 on 23 Jun 2005

--- rcthomas3000+cdbi@xxxxx.xxx wrote:


> 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 => ... } );
> 
> I just want to pass the string in the name column instead and have Show DWIM:
> 
>  my $showing = Show->create( 
>     { film => 'Clash of the Titans', theater => 'Cinemegaloplex III', ... }
> );
> 

Ahh. Are you wanting to store the names of the Films and Theatres in Show
rather than the ids so when you view that table you get usable output without
having to look in Film or Theater tables? Yet a Film and Theatre have many
Shows and you want to access those but Show does not store their pks so you can
not? 

If so, here is one way (this maybe exactly what tony said): do not specify any
relationships. They won't help. In Show you can use Film->search(title =>
$show->film) to get more details or Film->retrieve_all if you need films for a
select box.  In Film and Theatre write your own "shows" method . For Film it
could be

sub shows ( 
 return Shows->search(film => shift->title);
}

Altertatively you could write your own has_many relationship where you could
specify which pk to use in Film and what column it maps to in Show.

hth


pjs


		
__________________________________ 
Yahoo! Mail 
Stay connected, organized, and protected. Take the tour: 
http://tour.mail.yahoo.com/mailtour.html 

Re: Mapping column values to objects on create() or select().
Peter Speltz 20:38 on 23 Jun 2005

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