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

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
to:

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
$c->model('db::genders')->find({name=>'male'})->gender_id
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.

--john

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Search Discussions

  • Matt S Trout at May 26, 2006 at 3:21 am

    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
  • John Napiorkowski at May 26, 2006 at 3:45 am
    Matt,

    That did the trick, but I was a bit careful to try
    this before since the package was marked as
    experimental. Do you thing this is starting to settle
    down now or are we expecting big changes in the next
    few revs of DBIx?

    Thanks for such a quick reply, btw.

    --john

    --- Matt S Trout wrote:
    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
    _______________________________________________
    List:
    http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
    Wiki: http://dbix-class.shadowcatsystems.co.uk/
    IRC: irc.perl.org#dbix-class
    SVN:
    http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
    >


    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • Matt S Trout at May 26, 2006 at 11:31 am

    John Napiorkowski wrote:
    Matt,

    That did the trick, but I was a bit careful to try
    this before since the package was marked as
    experimental. Do you thing this is starting to settle
    down now or are we expecting big changes in the next
    few revs of DBIx?
    I did't say "use ResultSetManager", I said "use a custom resultset
    class". The former is experimental, the latter is normal perl
    subclassing and as such is highly likely to go away any time soon (I
    hear subclasses will even be supported in perl6 ;)
  • John Napiorkowski at May 26, 2006 at 1:28 pm
    Thanks Matt. I got this working correctly now and it
    seems very stable when I run all my tests. -john



    ## Create a videos object for reference.
    my $videos = $c->model("db::videos::videos");

    --- Matt S Trout wrote:
    John Napiorkowski wrote:
    Matt,

    That did the trick, but I was a bit careful to try
    this before since the package was marked as
    experimental. Do you thing this is starting to settle
    down now or are we expecting big changes in the next
    few revs of DBIx?
    I did't say "use ResultSetManager", I said "use a
    custom resultset
    class". The former is experimental, the latter is
    normal perl
    subclassing and as such is highly likely to go away
    any time soon (I
    hear subclasses will even be supported in perl6 ;)

    _______________________________________________
    List:
    http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
    Wiki: http://dbix-class.shadowcatsystems.co.uk/
    IRC: irc.perl.org#dbix-class
    SVN:
    http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
    >


    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • John Li at May 26, 2006 at 9:56 pm
    Hi,

    Is there anyway that we can specify tables that we want/don't want in
    DBIx::Class::Schema::Loader::make_schema_at? Preferably using regular
    expressions or an arrayref of table names.

    For example:

    use DBIx::Class::Schema::Loader qw/ make_schema_at /;
    make_schema_at(
    'New::Schema::Name',
    { relationships => 1,
    debug => 1,
    dump_directory => './lib',
    tables => ['table1', 'table2', "<some_regex>"], #
    something like this.
    },
    [ 'dbi:Pg:dbname="foo"','postgres' ],
    );


    Thanks,

    John (Zhuang) Li
  • John Li at May 26, 2006 at 10:19 pm
    Hi,



    Is there anyway that we can specify tables that we want/don't want in
    DBIx::Class::Schema::Loader::make_schema_at? Preferably using regular
    expressions or an arrayref of table names.



    For example:



    use DBIx::Class::Schema::Loader qw/ make_schema_at /; make_schema_at(

    'New::Schema::Name',

    { relationships => 1,

    debug => 1,

    dump_directory => './lib',

    tables => ['table1', 'table2', "<some_regex>"], #

    something like this.

    },

    [ 'dbi:Pg:dbname="foo"','postgres' ], );





    Thanks,



    John (Zhuang) Li



    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.rawmode.org/pipermail/dbix-class/attachments/20060526/aa5ddde6/attachment.htm
  • Matt S Trout at May 26, 2006 at 11:20 pm

    John Li wrote:
    Hi,



    Is there anyway that we can specify tables that we want/don't want in
    DBIx::Class::Schema::Loader::make_schema_at? Preferably using regular
    expressions or an arrayref of table names.
    Y'know, this is documented :)

    http://search.cpan.org/~blblack/DBIx-Class-Schema-Loader-0.03000/lib/DBIx/Class/Schema/Loader.pm#loader_options

    references

    ::Base, which includes

    http://search.cpan.org/dist/DBIx-Class-Schema-Loader/lib/DBIx/Class/Schema/Loader/Base.pm#constraint

    which takes a regex. That do the trick?
  • John Li at May 26, 2006 at 11:28 pm
    Thanks, Matt.

    I also found 'constraint' and 'exclude' in the code before getting this
    message.

    foreach my $table (@tables) {
    my $constraint = $self->constraint;
    my $exclude = $self->exclude;

    next unless $table =~ /$constraint/;
    next if defined $exclude && $table =~ /$exclude/;
    ...
    }

    Thanks again for pointing out the documentation :)

    John


    -----Original Message-----
    From: dbix-class-bounces at lists.rawmode.org
    [mailto:dbix-class-bounces at lists.rawmode.org] On Behalf Of Matt S Trout
    Sent: Friday, May 26, 2006 4:20 PM
    To: dbix-class at lists.rawmode.org
    Subject: Re: [Dbix-class] Specify tables for DBIx::Class::Schema::Loader

    John Li wrote:
    Hi,



    Is there anyway that we can specify tables that we want/don't want in
    DBIx::Class::Schema::Loader::make_schema_at? Preferably using regular
    expressions or an arrayref of table names.
    Y'know, this is documented :)

    http://search.cpan.org/~blblack/DBIx-Class-Schema-Loader-0.03000/lib/DBIx/Cl
    ass/Schema/Loader.pm#loader_options

    references

    ::Base, which includes

    http://search.cpan.org/dist/DBIx-Class-Schema-Loader/lib/DBIx/Class/Schema/L
    oader/Base.pm#constraint

    which takes a regex. That do the trick?

    _______________________________________________
    List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
    Wiki: http://dbix-class.shadowcatsystems.co.uk/
    IRC: irc.perl.org#dbix-class
    SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
  • Matt S Trout at May 27, 2006 at 12:15 am

    John Li wrote:
    Thanks, Matt.

    I also found 'constraint' and 'exclude' in the code before getting this
    message.

    foreach my $table (@tables) {
    my $constraint = $self->constraint;
    my $exclude = $self->exclude;

    next unless $table =~ /$constraint/;
    next if defined $exclude && $table =~ /$exclude/;
    ...
    }

    Thanks again for pointing out the documentation :)
    Yeah well, I had to actually read the docs first since I've never used a
    Loader and never plan to :)
  • Matt S Trout at May 26, 2006 at 10:20 pm
    John Li wrote:
    >
    <nothing to do with the mail he replied to>
    If it's a separate topic, write a new e-mail so you start a new thread.
    Replying to an existing one will fuck up the flow of conversation was
    everybody using a sensible mail client.
  • John Li at May 26, 2006 at 10:17 pm
    Sorry, Matt, that I forgot that. I'll do another one the way you just said.


    john

    -----Original Message-----
    From: dbix-class-bounces at lists.rawmode.org
    [mailto:dbix-class-bounces at lists.rawmode.org] On Behalf Of Matt S Trout
    Sent: Friday, May 26, 2006 3:21 PM
    To: dbix-class at lists.rawmode.org
    Subject: Re: [Dbix-class] Specify tables for DBIx::Class::Schema::Loader

    John Li wrote:
    >
    <nothing to do with the mail he replied to>
    If it's a separate topic, write a new e-mail so you start a new thread.
    Replying to an existing one will fuck up the flow of conversation was
    everybody using a sensible mail client.

    _______________________________________________
    List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
    Wiki: http://dbix-class.shadowcatsystems.co.uk/
    IRC: irc.perl.org#dbix-class
    SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbix-class @
categoriesperl, catalyst
postedMay 26, '06 at 3:03a
activeMay 27, '06 at 12:15a
posts12
users3
websitedbix-class.org
irc#dbix-class

People

Translate

site design / logo © 2021 Grokbase