[Dbix-class] Knowing the correct alias

Bill Moseley moseley at hank.org
Tue Nov 23 14:59:19 GMT 2010


Say I have a method in my base class shared by all resultset classes to add
an id onto a resultset criteria.

package ::Resultset;

sub limit_by_id {
    my ( $rs, $id ) =3D @_;
    return $rs->search( { 'me.id' =3D> $id } );
}

Now, I could do this:

$rs =3D $schema->resultset( 'Track' )->limit_by_id( 1234 );


But, the following won't work because the alias is wrong (should be '
tracks.id' =3D> $id):

$cd_rs =3D $schema->resultset( 'Cd' )->search( 'me.id' =3D> 4321 );
$rs =3D $cd_rs->search_related( 'tracks' )->limit_by_id( 1234 );


Would it be safe to use the resultset's current alias?

sub limit_by_id {
    my ($rs , $id ) =3D @_;

    my $alias =3D $rs->{attrs}{alias};
    return $rs->search({ "$alias.id" =3D> $id } );
}



-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20101123/2c0=
b2121/attachment.htm


More information about the DBIx-Class mailing list