[Dbix-class] tinyint and enum mapping

jackal me at jackal.in
Fri Oct 24 14:34:06 BST 2008


On Friday 24 October 2008 17:11:46 Fayland Lam wrote:
> hi, folks.
>
> basically I prefers tinyint better than enum becaue I don't need alter
> table when adding another value to col.
>
> see now we have a column 'status' and it's tinyint instead of enum
> then I want my code treats it as enum instead of tinyint. it means I
> can do something like follows:
>
> ->create( { status => 'banned' } );
> ->update( { status => 'warned' } );
> and ->delete and others.
>
> I think InflateColumn is what I want. but I just wonder if there is a
> plugin for that? so that I can just add it into load_components and
> set an attribute when add_columns.
> is there a component around? or should I write one? :)
>
> Thanks.

No, InflateColumn works with objects only. It cannot *modify* data.
It's destination - to deflate object into scalar for DB, and inflate scalar 
into object. Not to modify.

Module I want to contribute, ModifyColumn, can do it:

my @status = qw/ok banned warned something else/;
__PACKAGE__->modify_column('status', {
    inflate => sub { $status[$_[0]] },
    deflate => sub { 
        my $value = shift;
        for (my $i=0; $i<=$#status; $i++) {
            return $i if $status[$i] eq $value;
        }
        return undef;
    }
});

http://lists.scsys.co.uk/pipermail/dbix-class/2008-October/006968.html


-- 
Kind regards, Eugeny.



More information about the DBIx-Class mailing list