[Dbix-class] question about adding custom methods to DBIx Classes

Matt S Trout dbix-class at trout.me.uk
Fri May 26 05:21:28 CEST 2006


John Napiorkowski wrote:
> Hi,
> 
> I'm sure I'm just missing something obvious, but I'm
> having trouble adding some custom methods to a DBIx
> class.  I think I understand the issue but I was
> hoping someone could help clarify for me, since I get
> this to work sometime, but other times it doesnt.
> 
> So I have a DBIx class similar to this:
> 
> package myApp::Schema::members;
> 
> use warnings;
> use strict;
> 
> use base qw/DBIx::Class/;
> 
> __PACKAGE__->load_components(qw/ PK::Auto Core /);
> __PACKAGE__->table('members');
> __PACKAGE__->add_columns(qw/ id name email /);
> __PACKAGE__->set_primary_key(qw/ id /);
> 
> sub lc_name
> {
>    my $self  = shift @_;
> 
>    return lc $self->email;
> }
> 
> sub id_by_email
> {
>    my $self  = shift @_;
>    my $email = shift @_;
> 
>    return $self->find({email=>$email})->id;
> }
> 
> 
> Now, assuming that my 'email' column is specified as
> unique so I can do ->find on it I can see that the
> first function workings properly when I do (in
> catalyst)
> 
> my $lc_name = $c->model("db::members")->find([any
> valid id])->lc_name;
> 
> but the second function dies with a "Can't locate
> object method "id_by_email" via package
> "DBIx::Class::ResultSet" when I try:
> 
> my $id = $c->model("db::members")->id_by_email([any
> valid email]);

You want a custom resultset class.

http://search.cpan.org/~jrobinson/DBIx-Class-0.06003/lib/DBIx/Class/Manual/Cookbook.pod#Predefined_searches



More information about the Dbix-class mailing list