Re: Deleting from $film->stars in Many to Many Example

[prev] [thread] [next] [Date index for 2004/11/22]

From: Perrin Harkins
Subject: Re: Deleting from $film->stars in Many to Many Example
Date: 21:59 on 22 Nov 2004
On Mon, 2004-11-22 at 12:34, Peter Speltz wrote:
> Only one actor(star) per role. Role is defined like this:
>  Role->table('role');
>  Role->columns(Primary => qw/film actor/);
>  Role->has_a(film => 'Film');
>  Role->has_a(actor => 'Actor');

Ah, okay.  That makes "role" a lousy name for that table in my opinion,
since it implies to me that there is more than just a mapping in it,
like the name of the role.

Come to think of it, doesn't this mean there's no cascading delete? 
Those only happen when you delete something with a has_many
relationship, which Role doesn't have.

> Right, I want to delete the role not the actor. With stars defined as in the
> example
>     Film->has_many(stars => [ Role => 'actor' ]);
> then  when I say  
>     film->stars[2]->delete
> the actor get's deleted, not the role.  Not what i want. 

That's because the return value from stars() is a list of actors, not
roles.

> Whereas if stars were defined like this: 
>     Film->has_many ( 'stars' => "Role" ); 
> Then when i say 
>     film->stars[2]->delete
> the role gets deleted like i want it too.

But then you don't get Actors back when you call Film->stars().

You can always delete Roles directly:

Role->delete( actor => $actor, film => $film );

Or you could use the second approach (without mapping to Actors) and
code a separate method for getting the Actors efficiently without the
extra database calls.  It kind of depends on your usage pattern and what
feels more natural in your app. 

- Perrin

Re: Deleting from $film->stars in Many to Many Example
Perrin Harkins 21:59 on 22 Nov 2004

Generated at 17:24 on 02 Dec 2004 by mariachi v0.52