[Dbix-class] $c->stash for complex/cascaded relationship bridges

will trillich will.trillich at serensoft.com
Mon Jan 23 15:19:30 GMT 2012


Can you use related_resultset or search_related?

# See https://metacpan.org/module/DBIx::Class::ResultSet#related_resultset
$table4_rs =3D
$table1_rs->search_related_rs('table2')->search_related_rs('table4');

# See
https://metacpan.org/module/DBIx::Class::Manual::Features#search_related
$table4_rs =3D $table1_rs
    ->search_related_rs( table2 =3D> { fld =3D> 'blah' } )
    ->search_related_rs('table4' =3D> { xyz =3D> 'pdq' } );

You can also inject utility methods into your schema definitions to return
various related recordsets as well.



On Mon, Jan 23, 2012 at 8:55 AM, Roland Philibert <rphilibert at aptina.com>wr=
ote:

> I manage to go through the database from a given primary key by doing in
> the controller:
>
> ...
>    my $pk =3D '1';
>    my $t1 =3D $c->model('DB')->resultset('Table1')->search({ table1id =3D>
> $pk,},
>                                                          {result_class
> =3D> 'DBIx::Class::ResultClass::HashRefInflator',});
>
>    my $rs_t2 =3D $c->model('DB')->resultset('Table2')->search({
> 'table3s.ftable1id' =3D> $pk ,},
>                                                          {join =3D> {
> 'table3s'},
>
> result_class =3D> 'DBIx::Class::ResultClass::HashRefInflator',});
>
>    while ( my $subrs =3D $rs_t2->next ) {
>       $subrs =3D $c->model('DB')->resultset('Table2')->search(   {
> table2id =3D> $subrs->{table2id},  },   { } );
>       push @t2 , $subrs;
>    }
>    $c->stash(
>                t1 =3D> [ $t1 ] ,
>                rst2 =3D> \@t2
>             );
>    $c->detach($c->view("HTML"));
> }
>
>
>
> ..so that's a lof of searches,
> The first one is to get the info related to table1.
> The second one is to get all related table2 items related to pk of
> table1.
> The loop create a search for each items of the second search to get
> items from table4.
>
> ....keep thinking that there must be a simpler way?
> Any help greatly appreciated.
>
> Thanks
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Aptina (UK) Limited, Century Court, Millennium Way, Bracknell, Berkshire,
> RG12 2XT. Registered in England No. 06570543.
>
> This e-mail and any attachments contain confidential information and are
> solely for the review and use of the intended recipient. If you have
> received this e-mail in error, please notify the sender and destroy this
> e-mail and any copies.
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>



-- =

"We act as though comfort and luxury were the chief requirements of life,
when all that we need to make us happy is something to be enthusiastic
about." -- Albert Einstein
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20120123/478=
80d8e/attachment.htm


More information about the DBIx-Class mailing list