[Catalyst] How to access DBIC schema from class method?

seasprocket at gmail.com seasprocket at gmail.com
Wed Aug 11 02:59:55 GMT 2010


On Tue, Aug 10, 2010 at 6:34 PM, will at serensoft.com <will at serensoft.com>wro=
te:

> Not sure why you'd have a create-self method inside the instance itself.
> Usually our paradigm is something like
>
> It's not uncommon for objects to require fairly complex validation and/or
initialization. I've gotten used to doing that inside a class method during
which actual object creation happens. I suppose it could just as well happen
in two steps:

my $user =3D $c->model('MyApp::User')->create();
$user->init($params);

But still my general question remains. I can't quite figure out how DBIC
works ...


> my $user =3D $c->model('MyApp::User')->find_or_new( { id =3D> $id } );
> $user->password( $newpass );
> $user->somefield( $newval );
> #etc
> $user->update;
>
>
> On Tue, Aug 10, 2010 at 7:57 PM, <seasprocket at gmail.com> wrote:
>
>> I'm confused about the "right" way to get a schema instance from within a
>> class method. If I've already got a resultset or resultsource instance, =
it's
>> easy. But from a class method within my model? Do I create a new connect=
ion
>> every time I need one, ie. MyApp::Schema->connect()?
>>
>> To be specific:
>>
>> package MyApp::Schema::Result::User;
>> use base 'DBIx::Class::Core';
>> ...
>>
>> sub create_user {
>>     my ($class, $params) =3D @_;
>>     # Want to call $user_rs->create() here after I've validated data.
>>     # my $schema =3D MyApp::Schema->connect()?
>> }
>>
>> My suspicion is that this should be an instance method, bc it represents
>> the table, not a user object. And the way to get an instance is to call
>> $rs->result_source on a result set. But when I call:
>>       $c->model('DB::User')->result_source->create_user($params) it fails
>> with the msg:
>>
>> Can't locate object method "create_user" via package
>> "DBIx::Class::ResultSource::Table"
>>
>>
>>
>>
>>
>>
>> --
>> Bikewise: http://www.bikewise.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.uk/
>> Dev site: http://dev.catalyst.perl.org/
>>
>>
>
>
> --
> will trillich
> "Sometimes life's gonna hit you in the head with a brick. Don't lose
> faith." -- Steve Jobs
>
> --
> Bikewise: http://www.bikewise.org
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100810/e1883=
eae/attachment.htm


More information about the Catalyst mailing list