[Dbix-class] has_many relationship docs

Toby Corkindale tjc at wintrmute.net
Fri Apr 28 17:37:26 CEST 2006


On Thu, Apr 27, 2006 at 08:10:16PM +0100, Matt S Trout wrote:
> Toby Corkindale wrote:
> > Did anyone come back (perhaps offlist) with an answer to this?
> > 
> > Reason I ask is that I'm just trying to get my head around adding objects via
> > many_to_many relationships. Is there a shortcut method, a-la has_many's
> > *_add_to() method?
> > 
> > ie.
> > Actor has a many_to_many relationship to Roles, eg:
> > Schema::Actor->many_to_many( roles => 'actor_roles', 'Schema::Roles');
> > Is it possible to do something like:
> > $actor->add_to_roles( { role => 'Sherlock Holmes' } );
> > ?
> 
> $actor->roles->create(...);
> 
> if you really want the same behaviour as has_many, you could write
> 
> sub add_to_roles { shift->roles->create(@_); }
> 
> which is basically what the has_many rel generates :)

Thanks - I ended up with a helper function, a bit like that.
It became more complicated because the many-to-many relationships were along
the lines of:
A, B, and C, all have many-to-many relationships with Z.
(So, three linking tables - A-to-Z, B-to-Z, C-to-Z)

The only problem was that A, B, and C, all had a differently named auto-created
add_to_ function (add_to_a_to_z, add_to_b_to_z, etc) because of my schema
inplementation, but since I needed a helper function anyway, I could just call
it the same thing in each table class of the schema, and then use the generic
$foo->add_new_metadata($Z) that I wanted to be able to do.

CHeers,
Toby


-- 
Turning and turning in the widening gyre/The falcon cannot hear the falconer;
Things fall apart, the centre cannot hold/Mere anarchy is loosed upon the world
(gpg --keyserver www.co.uk.pgp.net --recv-key B1CCF88E)



More information about the Dbix-class mailing list