[Dbix-class] Help with Many to Many Relationships

Ian Wells ijw at cack.org.uk
Wed Jun 17 11:41:37 GMT 2009


2009/6/17 Dave Cross <dave at dave.org.uk>:
> I've having a brain freeze trying to implement a query in DBIC. I'm sure I'm
> missing something obvious.
>
> Here's the situation. Assume standard DBIC classes (created by
> Schema::Loader).
>
> Four tables - Director, Film, Actor, ActorInFilm
>
> A Director directs many Films.
> A Film has many Actors
> An Actor is in Films
>
> So the ActorInFilm table models the many-to-many relationship between Actors
> and Films.
>
> My problem is: given an actor and a director, get the list of films that
> they worked on together. So the result will be a resultset containing Film
> objects (or, alternatively, a list of Film objects).


package Actor;

__PACKAGE->has_many('actorinfilms',
'ActorInFilm', { 'foreign.actor' => 'self.actor_id' });

package ActorInFilm;

__PACKAGE->belongs_to('film' => 'Film', { 'foreign.actor' => 'self.actor_id' });



More information about the DBIx-Class mailing list