[Catalyst] fetching a model instance from a model instance
Csongor Fagyal
concept at conceptonline.hu
Thu Oct 27 03:24:31 GMT 2011
Hi All,
Please excuse me if I ask lame questions. There's just so much
documentation lying around that it's hard to find what I am looking for.
--------------
Basically I have a (generated) DBIx::... model, and I would like to
fetch another instance from it. (What I am trying to achieve is to make
the model verify a confirmation code for a user, and return the
corresponding user object if the code is right.)
So... it looks like this:
# somewhere in a controller:
my $result =
$c->model('DB::User')->new_result({})->confirmAndGetUser($c,$code);
# Then in User.pm:
package MyApp::Schema::Result::User;
use utf8;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
# ... etc ....
sub confirmAndGetUser {
my ($self,$c,$code) = @_;
# ... generate $uid from $code, etc...
my $user = $c->model("DB::User")->find($uid);
$user || return 'Wrong verification code.";
# ... etc ...
return $user;
}
My problem is that I pass $c to the model, which looks like a bad idea
in general. (It's ugly at least.) But how can I do a find(...)
otherwise? Can I do something like
$self->something->somethingelse->andwhatnoat('User')->find( ... ) ?
As the matter of fact, the whole thing would look much better to me as a
class method instead of an instance method. Something like this:
my $user = MyApp::Schema::Result::User->confirmAndGetUser($code);
Is that doable? (Doesn't feel like it...)
BTW, "you should do this differently, like this: ..." is also an
acceptable answer :) [ Maybe what I am trying to do should go somewhere
else... not in the controller, not in the model, but... ? ]
Also, if this is a FAQ item, please point me to that FAQ :)
Thank you,
- Fagzal
More information about the Catalyst
mailing list