2 DBIx::Class::CDBICompat::ColumnsAsHash;
10 DBIx::Class::CDBICompat::ColumnsAsHash
14 See DBIx::Class::CDBICompat for directions for use.
18 Emulates the I<undocumnted> behavior of Class::DBI where the object can be accessed as a hash of columns. This is often used as a performance hack.
20 my $column = $row->{column};
22 =head2 Differences from Class::DBI
24 If C<DBIC_CDBICOMPAT_HASH_WARN> is true it will warn when a column is accessed as a hash key.
31 my $new = $class->next::method(@_);
33 $new->_make_columns_as_hash;
41 my $new = $class->next::method(@_);
43 $new->_make_columns_as_hash;
49 sub _make_columns_as_hash {
52 for my $col ($self->columns) {
53 if( exists $self->{$col} ) {
54 warn "Skipping mapping $col to a hash key because it exists";
57 tie $self->{$col}, 'DBIx::Class::CDBICompat::Tied::ColumnValue',
63 package DBIx::Class::CDBICompat::Tied::ColumnValue;
66 use Scalar::Util qw(weaken isweak);
70 my($class, $obj, $col) = @_;
71 my $self = [$obj, $col];
74 return bless $self, $_[0];
79 my($obj, $col) = @$self;
83 carp "Column '$col' of '$class/$id' was fetched as a hash"
84 if $ENV{DBIC_CDBICOMPAT_HASH_WARN};
86 return $obj->column_info($col)->{_inflate_info}
87 ? $obj->get_inflated_column($col)
88 : $obj->get_column($col);
93 my($obj, $col) = @$self;
97 carp "Column '$col' of '$class/$id' was stored as a hash"
98 if $ENV{DBIC_CDBICOMPAT_HASH_WARN};
100 return $obj->column_info($col)->{_inflate_info}
101 ? $obj->set_inflated_column($col => shift)
102 : $obj->set_column($col => shift);