[Catalyst] fetching a model instance from a model instance

Csongor Fagyal concept at conceptonline.hu
Thu Oct 27 11:26:16 GMT 2011


Hi,
> Hello Fragzal,
>
> a Result basicly represents a single row, while ResultSet is an object =

> which represents a database query.
> Your should put your confirmAndGetUser method into =

> MyApp::Schema::ResultSet::User, like:
>
> package MyApp::Schema::Result::User;
> use Moose;
> BEGIN { extends 'DBIx::Class::ResultSet'; }
>
> sub confirmAndGetUser {
>     my ($self, $code) =3D @_;
>
>     return $self->find({ code =3D> $code });
> }
>
> ....
>
> and than within your Controller: my $result =3D =

> $c->model('DB::User')->confirmAndGetUser($code);
I don't really follow you.

I have generated the models with the helper script, and it generated a =

MyApp::Model::DB for me, which extends Catalyst::Model::DBIC::Schema, =

and also MyApp::Schema::Result::User, which extends DBIx::Class::Core:

--------
package MyApp::Schema::Result::User;
use utf8;
use Digest::MD5 qw/md5_hex/;
use Moose;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Core';
---------

I don't have a MyApp::Schema::Result*Set*::User. So... now what? :)


Thanks,
- Fagzal

>
> Cheers,
> Dimi
>
> On Thu, Oct 27, 2011 at 5:24 AM, Csongor Fagyal =

> <concept at conceptonline.hu <mailto:concept at conceptonline.hu>> wrote:
>
>     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 =3D
>     $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) =3D @_;
>
>        # ... generate $uid from $code, etc...
>
>        my $user =3D $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 =3D 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
>
>
>     _______________________________________________
>     List: Catalyst at lists.scsys.co.uk <mailto:Catalyst at lists.scsys.co.uk>
>     Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>     Searchable archive:
>     http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>     Dev site: http://dev.catalyst.perl.org/
>
>
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.u=
k/
> Dev site: http://dev.catalyst.perl.org/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20111027/ca944=
9f2/attachment.htm


More information about the Catalyst mailing list