2 DBIx::Class::CDBICompat::ColumnsAsHash;
8 use Scalar::Util qw(weaken);
14 DBIx::Class::CDBICompat::ColumnsAsHash
18 See DBIx::Class::CDBICompat for directions for use.
22 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.
24 my $column = $row->{column};
26 =head2 Differences from Class::DBI
28 This will warn when a column is accessed as a hash key.
35 my $new = $class->next::method(@_);
37 $new->_make_columns_as_hash;
45 my $new = $class->next::method(@_);
47 $new->_make_columns_as_hash;
53 sub _make_columns_as_hash {
57 for my $col ($self->columns) {
58 if( exists $self->{$col} ) {
59 warn "Skipping mapping $col to a hash key because it exists";
62 next unless $self->can($col);
63 $self->{$col} = defer {
64 my $class = ref $self;
65 carp "Column '$col' of '$class/$self' was accessed as a hash";
74 for my $col ($self->columns) {
75 if( $self->_hash_changed($col) ) {
76 my $class = ref $self;
77 carp "Column '$col' of '$class/$self' was updated as a hash";
78 $self->$col($self->_get_column_from_hash($col));
79 $self->{$col} = defer { $self->$col() };
83 return $self->next::method(@_);
89 return 0 unless exists $self->{$col};
91 my $hash = $self->_get_column_from_hash($col);
92 my $obj = $self->$col();
94 return 1 if defined $hash xor defined $obj;
95 return 0 if !defined $hash and !defined $obj;
96 return 1 if $hash ne $obj;
100 # get the column value without a warning
101 sub _get_column_from_hash {
102 my($self, $col) = @_;
104 local $SIG{__WARN__} = sub {};
105 return force $self->{$col};