1 package DBIx::Class::PK;
6 use base qw/DBIx::Class::Row/;
10 DBIx::Class::PK - Primary Key class
16 This class contains methods for handling primary keys and methods
25 return join(" AND ", map { "$_ = ?" } $class->primary_columns);
30 return (map { $self->{_column_data}{$_} } $self->primary_columns);
33 =head2 discard_changes
35 Re-selects the row from the database, losing any changes that had
42 delete $self->{_dirty_columns};
43 return unless $self->in_storage; # Don't reload if we aren't real!
44 my ($reload) = $self->result_source->resultset->find
45 (map { $self->$_ } $self->primary_columns);
46 unless ($reload) { # If we got deleted in the mean-time
50 delete @{$self}{keys %$self};
51 @{$self}{keys %$reload} = values %$reload;
57 Returns the primary key(s) for a row. Can't be called as
64 $self->throw_exception( "Can't call id() as a class method" )
66 my @pk = $self->_ident_values;
67 return (wantarray ? @pk : $pk[0]);
72 Returns a unique id string identifying a row object by primary key.
73 Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
74 L<DBIx::Class::ObjectCache>.
80 $self->throw_exception( "Can't call ID() as a class method" )
82 return undef unless $self->in_storage;
83 return $self->_create_ID(map { $_ => $self->{_column_data}{$_} }
84 $self->primary_columns);
88 my ($self,%vals) = @_;
89 return undef unless 0 == grep { !defined } values %vals;
90 return join '|', ref $self || $self, $self->result_source->name,
91 map { $_ . '=' . $vals{$_} } sort keys %vals;
94 =head2 ident_condition
96 my $cond = $result_source->ident_condition();
98 my $cond = $result_source->ident_condition('alias');
100 Produces a condition hash to locate a row based on the primary key(s).
104 sub ident_condition {
105 my ($self, $alias) = @_;
107 $cond{(defined $alias ? "${alias}.$_" : $_)} = $self->get_column($_)
108 for $self->primary_columns;
116 Matt S. Trout <mst@shadowcatsystems.co.uk>
120 You may distribute this code under the same terms as Perl itself.