Create a custom accessor/mutator

9 views
Skip to first unread message

Chuck Zumbrun

unread,
Apr 14, 2017, 9:19:29 AM4/14/17
to Rose::DB::Object
Is it possible to create a custom accessor/mutator for a column?  For example, using the products table from the Object tutorial, I want to create my own name function so that I can code

$p = Product->new;
$p->name();

and it will call my name function.

package Product;

  use strict;

  use base 'My::DB::Object';

  __PACKAGE__->meta->setup
  (
    table => 'products',

    columns =>
    [
      id           => { type => 'integer', not_null => 1 },
      name         => { type => 'varchar', length => 255, not_null => 1 },
...
    ],

    primary_key_columns => [ 'id' ],

    unique_keys => [ 'name' ],

    allow_inline_column_values => 1,
  );

  1;

Peter Karman

unread,
Apr 14, 2017, 9:59:28 AM4/14/17
to rose-db...@googlegroups.com
Chuck Zumbrun wrote on 4/14/17 8:19 AM:
> Is it possible to create a custom accessor/mutator for a column? For example,
> using the products table from the Object tutorial, I want to create my own name
> function so that I can code
>
> $p = Product->new;
> $p->name();
>
> and it will call my name function.
>

check out
https://metacpan.org/pod/Rose::DB::Object::Metadata::Column#preserve_existing-BOOL

I think you can just define a 'name' method in your Product class in the normal
Perl way and then tell RDBO what to do about it.

You might want to use the `alias' config in that same class to alias the real
'name' column to some other name so that you can still get/set the column value.


--
Peter Karman . https://karpet.github.io . https://keybase.io/peterkarman

Chuck Zumbrun

unread,
Apr 14, 2017, 12:43:56 PM4/14/17
to Rose::DB::Object, pe...@peknet.com
alias was exactly what I needed.  Thanks!
Reply all
Reply to author
Forward
0 new messages