Applying Result's methods to the whole ResultSet

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Applying Result's methods to the whole ResultSet

Vladimir Melnik
Hello,

And one more question, if anyone doesn't mind. :-)

Is there an elegant way to apply a Result's method to the whole ResultSet?

In other words, here's what I have to do:
        # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
        my $rs_foo = $schema->resultset('Foo')->search({ ... });
        foreach my $r_foo ($rs_foo->all) {
            foreach my $r_bar ($r_foo->bars->all) {
                $r_bar->some_method;
            }
        }

...or something like like that:
        # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
        my $rs_foo = $schema->resultset('Foo')->search({ ... });
        foreach my $r_bar (map { $_->bars->all } $rs_foo->all) {
            $r_bar->some_method;
        }

...and here's what I would like to do:
        # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
        my $rs_foo = $schema->resultset('Foo')->search({ ... });
        foreach my $r_bar ($rs_foo->bars->all) {
            $r_bar->some_method;
        }

Do I have to add the "bars" method to the MyApp::Schema::ResultSet::Foo class manually?

Thanks in advance!

--
V.Melnik

_______________________________________________
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@...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Applying Result's methods to the whole ResultSet

James Gilbert
If I was developing the schema, in addition to the relation you show, in Result::Bar I'd have:

  __PACKAGE__->belongs_to('foo', 'MyApp::Schema::Result::Foo', { ... });

Then search for all the bars with a particular foo (you use the name of the relation, not the table) property:

  foreach my $r_bar ($schema->resultset('Bar')->search({ 'foo.x' => 'Baz' })) {
    $r_bar->some_method;
  }

> On 18 Apr 2017, at 16:59, Vladimir Melnik <[hidden email]> wrote:
>
> Hello,
>
> And one more question, if anyone doesn't mind. :-)
>
> Is there an elegant way to apply a Result's method to the whole ResultSet?
>
> In other words, here's what I have to do:
> # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
> my $rs_foo = $schema->resultset('Foo')->search({ ... });
> foreach my $r_foo ($rs_foo->all) {
>    foreach my $r_bar ($r_foo->bars->all) {
>        $r_bar->some_method;
>    }
> }
>
> ...or something like like that:
> # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
> my $rs_foo = $schema->resultset('Foo')->search({ ... });
> foreach my $r_bar (map { $_->bars->all } $rs_foo->all) {
>    $r_bar->some_method;
> }
>
> ...and here's what I would like to do:
> # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
> my $rs_foo = $schema->resultset('Foo')->search({ ... });
> foreach my $r_bar ($rs_foo->bars->all) {
>    $r_bar->some_method;
> }
>
> Do I have to add the "bars" method to the MyApp::Schema::ResultSet::Foo class manually?
>
> Thanks in advance!
>
> --
> V.Melnik



--
 The Wellcome Trust Sanger Institute is operated by Genome Research
 Limited, a charity registered in England with number 1021457 and a
 company registered in England with number 2742969, whose registered
 office is 215 Euston Road, London, NW1 2BE.

_______________________________________________
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@...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Applying Result's methods to the whole ResultSet

James Gilbert
In reply to this post by Vladimir Melnik
Oh, and this will work, almost.  You don't need the "->all".  The has_many('bars') relation creates a 'bars' method on each Foo Result object:

  foreach my $r_bar (map { $_->bars } $rs_foo->all) {
    $r_bar->some_method;
  }

For speed, prefetch bars.

> On 18 Apr 2017, at 16:59, Vladimir Melnik <[hidden email]> wrote:
>
> ...or something like like that:
> # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
> my $rs_foo = $schema->resultset('Foo')->search({ ... });
> foreach my $r_bar (map { $_->bars->all } $rs_foo->all) {
>    $r_bar->some_method;
> }



--
 The Wellcome Trust Sanger Institute is operated by Genome Research
 Limited, a charity registered in England with number 1021457 and a
 company registered in England with number 2742969, whose registered
 office is 215 Euston Road, London, NW1 2BE.

_______________________________________________
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@...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Applying Result's methods to the whole ResultSet

Matt S Trout-2
In reply to this post by Vladimir Melnik
On Tue, Apr 18, 2017 at 06:59:48PM +0300, Vladimir Melnik wrote:
> Hello,
>
> And one more question, if anyone doesn't mind. :-)
>
> Is there an elegant way to apply a Result's method to the whole ResultSet?
>
> In other words, here's what I have to do:
> # MyApp::Schema::Result::Foo->has_many('bars', 'MyApp::Schema::Result::Bar', { ... });
> my $rs_foo = $schema->resultset('Foo')->search({ ... });

  $_->some_method for map $_->bars->all, $rs_foo->all;

or

  $_->some_method for $rs_foo->search_related('bars')->all;

(I would always explicitly call the ->all for clarity)

--
Matt S Trout - Shadowcat Systems - Perl consulting with a commit bit and a clue

http://shadowcat.co.uk/blog/matt-s-trout/   http://twitter.com/shadowcat_mst/

Email me now on mst (at) shadowcat.co.uk and let's chat about how our CPAN
commercial support, training and consultancy packages could help your team.

_______________________________________________
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@...
Loading...