[Dbix-class] Applying Result's methods to the whole ResultSet
James Gilbert
jgrg at sanger.ac.uk
Tue Apr 18 16:39:44 GMT 2017
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 <v.melnik at uplink.ua> 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.
More information about the DBIx-Class
mailing list