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

John Napiorkowski jjn1056 at yahoo.com
Fri May 26 05:03:10 CEST 2006


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__->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

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]);

So I figured that when a function is called more like
a class method than an instance method I need to do
__PACKAGE__, rather than $self, which is how I usualy
handle class methods.  So I changed the second funtion

sub id_by_email
   my $self  = shift @_;
   my $email = shift @_;

   return __PACKAGE__->find({email=>$email})->id;

But (in catalyst) my error remains the same.  So I am
wondering if this has something to do with how
catalyst does an inplicit ->resultset for you?  Or I
am just not getting the way this works (probably the
case :) ).

I'd like to get this to work because I find myself
doing a lot of ...->find({[unique_col]=>$value})->id
and I'd like to clean it up a bit, particularly for
the tables that are just a list of standard constants,
like the gender table I have (if I have to do
one more time I will go crazy).  So I am wondering if
anyone out there has run into a similar issue?

Sorry if this should be on the catalyst list, but I
didn't want to crosspost and this list seemed the
better choice.


Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Dbix-class mailing list