简体   繁体   中英

DBIx::Class abstract parent ResultSet

I am trying to figure out a clean OO way to implement a problem I have run into with DBIx::Class. I have a User table that contains information common to all users. Each user can also have many different classes each which has its own unique required information. So for example a User may be an Admin and an Author. There are separate tables for the admin and author classes.

What I want to be able to do is create a common base class to access all of the classes from the user object. So a base class called Schema::UserClass and two subclasses called Schema::UserClass::Admin and Schema::UserClass::Author. What I would like to be able to do are things like:

# Get current user
my $user = MyApp->get_user();

# Get user classes
my @classes = $user->classes->all();
for my $class (@classes) {
    # Print class name
    print $class->name;
}

A similar problem is presented here: http://dbix-class.35028.n2.nabble.com/OO-advice-do-a-subclass-do-something-else-td5614176.html . But the solution is subpart in my opinion since it requires adding a new relationship for each class.

I don't see how a relationship can be made to the base class with knowledge of all of the sub classes. Any help would be much appreciated.

The solution I found isn't great either but it does work. If I get time I may consolidate this code in a CPAN module to make it a bit prettier.

package ParentSchema::Result::Class;

use strict;
use warnings;

use parent 'DBIx::Class::Core';

__PACKAGE__->add_columns(
  "user_id",
  {
    data_type => "integer",
    size => 32,
    is_foreign_key => 1,
    is_auto_increment => 0,
    is_nullable => 0,
    default_value => '',
  },
);

# stuff common to all schemas

__PACKAGE__->belongs_to(
  "user",
  "Schema::Result::User",
  { 'foreign.id' => "self.user_id" },
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
);

1;

package Schema::Result::Class::Author

use strict;
use warnings;

use parent 'ParentSchema::Class';

__PACKAGE__->table('class_author');

# class spesific stuff

__PACKAGE__->meta->make_immutable;

1;


package Schema::Result::User;

use strict;
use warnings;

use parent 'DBIx::Class::Core';

use Module::Pluggable::Object;
use String::CamelCase qw(decamelize);

__PACKAGE__->add_columns(
  "id",
  {
    data_type => "integer",
    size => 32,
    is_auto_increment => 1,
    is_nullable => 0,
    default_value => '',
  },
);

my $class_path = 'Schema::Result::Class';

my $mp = Module::Pluggable::Object->new(
    search_path => [ $class_path ]
);
my @class_plugins = $mp->plugins;

foreach my $class (@class_plugins) {
  (my $name = $class) =~ s/^\Q${class_path}\E//;
  __PACKAGE__->might_have(
    decamelize($name),
    $class,
    { "foreign.user_id" => "self.id" },
    { cascade_copy => 0, cascade_delete => 0 },
  );
}

__PACKAGE__->meta->make_immutable;

1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM