[Dbix-class] DBIx::Class creating get_* and set_* methods

Peter Edwards peter at dragonstaff.com
Fri Feb 8 07:47:26 GMT 2008


Steve K wrote:
>I created a post on Perl Monks regarding this
>question(http://www.perlmonks.org/?node_id=666868) and someone referred to
this mailing list.
>
>I want to methods that begin with 'get_' or 'set_' (depending on their
functionality).
># Accessor method
>$object->get_username;
># mutator method
>$object->set_username('bob');
>Has anyone done anything for this for DBIx::Class?

http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Row.pm#g
et_column
http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Row.pm#s
et_column

my $rs = $schema->resultset('User')->search({});
if ( $rs ) {
  for ( $rs->all ) {
    print $rs->get_column('username'), "\n";
    # either
    $rs->set_column('username', 'Bobo The Clown');
    $rs->update;
    # or  $rs->update({ username => 'Bobo The Clown' });
  }
}

jrockway says on perlmonks:
>Never use get_column and set_column directly unless you know what the
side-effects are. They don't work the same way as >using the accessors.
Inflation is skipped, resultsets are not returned for relationships, etc.

If you want inflation:

http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Row.pm#g
et_inflated_columns
http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Row.pm#s
et_inflated_columns
or
http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Row.pm#u
pdate

When your field name is the same as a Row method name, e.g. "delete", I
guess you pretty much have to use get_column() or get_inflated_columns() ?


Regards, Peter
http://perl.dragonstaff.co.uk




More information about the DBIx-Class mailing list