[Catalyst] Model/Schema questions

Matt S Trout dbix-class at trout.me.uk
Sun May 21 22:43:04 CEST 2006


Patryk Dwórznik wrote:
> 
> On May 21, 2006, at 9:00 PM, Matt S Trout wrote:
>>
>> Not unfortunately; that's by design. If you want to add a register()
>> method that creates an object, create a custom resultset class -
> 
> I know it's by design, but seemed unfortunate for me at the moment. :-)
> Thanks for explanation, obviously I didn't realize it was also a problem 
> with a class method itself.

No worries, people often find not being able to write class methods 
unintuitive to start with. Thing is, since the idea is to be able to 
call any resultset method on any resultset object - whether one that 
represents the entire table or a restricted one from search, it's the 
only sane way to do it. For example, having added register(...) as a 
resultset method you could do (contrived example)

# in your RS class

sub mass_register {
   my ($self, @usernames) = @_;
   return map { $self->register({ username => $_ }) } @usernames;
}

# then in controller code

sub mass_add_to_group :Local {
   my ($self, $c, $group) = @_;
   my @usernames = grep { defined && length }
                     @{$c->req->param('username')||[]};
   my $rs = $c->model('DB::User')->search({ group_id => $group });
   $rs->mass_register(@usernames);
}

and hitting /controller/mass_add_to_group/admin with a suitable form 
would Do The Right Thing (i.e. the group_id would be filled out on all 
the new user records created).

And of course, there's the whole 
more-than-one-connection-per-schema-class thing - having to keep a 
schema object around seems annoyingly inconvenient to begin with, but 
several years' experience of Class::DBI taught me that it's far, far 
more annoying to have to redo all your code to deal with a 
global-variable hack or similar to handle multiple connections when you 
do turn out to need them at some point :)



More information about the Catalyst mailing list