[Dbix-class] Applying Result's methods to the whole ResultSet

Vladimir Melnik v.melnik at uplink.ua
Tue Apr 18 15:59:48 GMT 2017


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



More information about the DBIx-Class mailing list