[Dbix-class] Result, ResultSet and load_namespaces

Alejandro Imass aimass at yabarana.com
Wed Jul 25 15:41:28 GMT 2012


Hi,

This is my first post to the list so.... does anyone know if there is
a searchable archive of this list?

This has probably been asked a thousand times but I haven't been able
to find an exact answer to this whilst STFW or RTFM.

We use DBIx::Class::Schema::Loader to create and update our Result
classes constantly. Instead of extending the Result classes below the
MD5 we decided a long time ago to use the ResultSet extension
mechanism, if I may call it that, so that load_namespaces will load
our extended classes automatically.

The problem that we've always had with this is that even though the
result set object created from the Result class is somehow associated
to the ResultSet class, if we want to use any of the methods in the
latter, we need to pass the Result instance. Example:

# table.pm by Loader and lives in the Result directory

package ns::Schema::Result::table;

use base 'DBIx::Class::Core';

...

# table.pm created by me in the ResultSet directory

package ns::Schema::ResultSet::table;

use base 'DBIx::Class::ResultSet';

sub foo {
   my $self = shift;

}
...


$instance = $schema->resultset('table')->find(1);

# This does NOT work:
$instance->foo();

# but instead the foo method must be called from the schema

$schema->resultset('table')->foo();

Of course, in foo() $self if the ResultSet instance and not the Result
instance so we need to pass $instance like so:

$schema->resultset('table')->foo($instance);

And our extended classes wind up being something like:

# table.pm in ResultSet

package ns::Schema::ResultSet::table;

use base 'DBIx::Class::ResultSet';

sub foo {
   my $self = shift;
   my $table = shift;

}
...

I have had several questions for a while and no doc seems to explain
this in detail:

1) When $schema->resultset('table')->foo() is executed which instances
are created:
    a) Just the ResultSet instance
    b) Both Result and ResultSet instances?
2) If 1-b then is there a way for both instances to see each other?
3) If 1-a what is the mod elegant way to connect both classes or instances?

Thanks beforehand for any pointers,

-- 
Alejandro Imass



More information about the DBIx-Class mailing list