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

2009-07-24 Thread Wallace Reis
On Thu, Jul 23, 2009 at 4:12 PM, Oleg Kostyukcub.ua...@gmail.com wrote:
 Hello all,

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

 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?

You can't define rels using constants in FK definitions like
'foreign.admin' = 1. You have to define just the 'all_users' rel with
{ 'foreign.company_id' = 'self.company_id' } and create result class
methods for 'users' and 'admins' like:
sub users_rs {
my $self = shift;
return $self-all_users_rs({ admin = '0' });
}

sub users {
my $self = shift;
my $rs = $self-users_rs;
return wantarray ? $rs-all : $rs;
}

sub admins_rs {
my $self = shift;
return $self-all_users_rs({ admin = '1' });
}

sub admins {
my $self = shift;
my $rs = $self-admins_rs;
return wantarray ? $rs-all : $rs;
}

Or you can create a base result class called Person, then Admin and
User classes as a views (CREATE VIEW) over Person, thus you can define
the rels like:
__PACKAGE__-has_many('admins', 'My::Schema::Result::User::Admin',
{ 'foreign.company_id' = 'self.company_id' });
__PACKAGE__-has_many('users', 'My::Schema::Result::User',
{ 'foreign.company_id' = 'self.company_id' });
__PACKAGE__-has_many('all_users', 'My::Schema::Result::Person',
{ 'foreign.company_id' = 'self.company_id' });

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

Just in User should be enough.


-- 
 wallace reis/wreis Catalyst and DBIx::Class consultancy with a clue
 Software Developer and a commit bit: 
http://shadowcat.co.uk/catalyst/
 Shadowcat Systems Limited
 http://www.shadowcat.co.uk http://www.linkedin.com/in/wallacereis

___
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk


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

2009-07-23 Thread Oleg Kostyuk
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)

___
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk