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