[Dbix-class] Relations for multi-class object inflation from one table

Oleg Kostyuk cub.uanic at gmail.com
Thu Jul 23 19:12:17 GMT 2009


Hello all,

I read about "Dynamic Sub-classing DBIx::Class proxy classes" in
DBIx::Class::Manual::Cookbook, and have question.

Consider we have following definitions (simplified):

package My::Schema;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_namespaces;

package My::Schema::Result::User;
use base qw/DBIx::Class/;
__PACKAGE__->load_components(qw/Core/);
__PACKAGE__->table('users');
__PACKAGE__->add_columns(qw/user_id ....../);
__PACKAGE__->set_primary_key('user_id');
sub inflate_result {
    # same as in DBIx::Class::Manual::Cookbook
}

package My::Schema::Result::User::Admin;
use base qw/My::Schema::Result::User/;
__PACKAGE__->table('users');

I want to have Company object, that should have 3 accessors, to access
plain users, admins, or all of them:

package My::Schema::Result::Company;
use base qw/DBIx::Class/;
__PACKAGE__->load_components(qw/Core/);
__PACKAGE__->table('companies');
__PACKAGE__->add_columns(qw/company_id ....../);
__PACKAGE__->set_primary_key('company_id');
__PACKAGE__->has_many('admins', 'My::Schema::Result::User::Admin', ????);
__PACKAGE__->has_many('users', 'My::Schema::Result::User', ????);
__PACKAGE__->has_many('all_users', 'My::Schema::Result::User', ????);

My questions is:

1) what should be instead of '????' in has_many() calls above? I
think, this should be something like this (for first has_many): {
'foreign.company_id' => 'self.company_id', 'foreign.admin' => 1 } - is
this correct?

2) my answer in (1) use details from User class (like constant values
- zero in previous question) - is it possible to avoid this, and
encapsulate all such details into one place (for example, User class)?

3) do I need write belongs_to(company) in User::Admin too, or only in
User will be enough?

4) do I need to use 'My::Schema::Result::User::Admin' in first
has_many, or this is incorrect, and only 'My::Schema::Result::User' is
acceptable?

Thanks.

-- 
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)



More information about the DBIx-Class mailing list