[CDBI] Re: custom accessor problem?

adam adamlounds at gmail.com
Wed Mar 14 13:54:50 GMT 2007


On 3/13/07, Edward J. Sabol <sabol at alderaan.gsfc.nasa.gov> wrote:
> Adam wrote:
> > It's in the Class::DBI pod -
> > http://search.cpan.org/~tmtm/Class-DBI-v3.0.16/lib/Class/DBI.pm#Changing_Your_Column_Accessor_Method_Names
> > so I didn't think I was being especially naughty :-)
>
> Oh, that's just a "passing mention," not real documentation. ;-) Sorry, yes,
> you're right. I still think you probably just need to specify the
> 'placeholder', an SQL fragment that can convert the Perl string back to a
> datetime datatype. Have you tried that yet?

The placeholder is only really relevant for INSERT/UPDATEs - I'm
specifically interested in SELECTs here, sorry.

> >> There are a couple entries in the CDBI Cookbook that you should read:
> >>
> >> http://wiki.class-dbi.com/wiki/Setting_system_date_fields
> >> http://wiki.class-dbi.com/wiki/Working_With_Oracle_Date_Fields
> >>
> >> Hope this helps, Ed
> >
> > Sort of - I guess I was hoping for a way to use custom sql for columns
> > rather than a specific solution to the datetime issue.
>
> Not sure what you mean. The examples in that first Wiki page are using custom SQL.

Yes, but I just wanted an accessor rather than a mutator - those
examples are all about setting date columns, not reading them in
'special' ways.


Right. I found the *real* reason behind this.

Compare the code in Class::DBI::accessor_name_for:
sub accessor_name_for {
        my ($class, $column) = @_;
        if ($class->can('accessor_name')) {
                warn "Use of 'accessor_name' is deprecated. Use
'accessor_name_for' instead\n";
                return $class->accessor_name($column)
        }
        return $column->accessor;
}

with the suggested override method in the docs:
http://search.cpan.org/~tmtm/Class-DBI-v3.0.16/lib/Class/DBI.pm#Changing_Your_Column_Accessor_Method_Names

 sub accessor_name_for {
    my ($class, $column) = @_;
    $column =~ s/^customer//;
    return $column;
  }

Note that the code says $column->accessor; the docs just say $column.

My superclass was overriding accessor_name_for() as documented, and
was manipulating/returning $column - the stringified version of which
is the *name* of the column, not the accessor. A quick tweak to my
accessor_name_for() to manipulate the accessor not the name and Bob's
your uncle.

Thanks for all your help, it's always useful to have encouragement to
delve into the code and work out what exactly is going on :-)
-- 
Adam



More information about the ClassDBI mailing list