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 (map { $self->{_column_data}{$_} } $self->primary_columns);
28 =head2 discard_changes ($attrs)
30 Re-selects the row from the database, losing any changes that had
33 This method can also be used to refresh from storage, retrieving any
34 changes made since the row was last read from storage.
36 $attrs is expected to be a hashref of attributes suitable for passing as the
37 second argument to $resultset->search($cond, $attrs);
42 my ($self, $attrs) = @_;
43 delete $self->{_dirty_columns};
44 return unless $self->in_storage; # Don't reload if we aren't real!
46 $attrs = { force_pool => 'master', %{ defined $attrs ? $attrs:{} } };
47 if( my $current_storage = $self->get_from_storage($attrs)) {
49 # Set $self to the current.
50 %$self = %$current_storage;
52 # Avoid a possible infinite loop with
53 # sub DESTROY { $_[0]->discard_changes }
54 bless $current_storage, 'Do::Not::Exist';
65 Returns the primary key(s) for a row. Can't be called as
72 $self->throw_exception( "Can't call id() as a class method" )
74 my @pk = $self->_ident_values;
75 return (wantarray ? @pk : $pk[0]);
80 Returns a unique id string identifying a row object by primary key.
81 Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
82 L<DBIx::Class::ObjectCache>.
88 $self->throw_exception( "Can't call ID() as a class method" )
90 return undef unless $self->in_storage;
91 return $self->_create_ID(map { $_ => $self->{_column_data}{$_} }
92 $self->primary_columns);
96 my ($self,%vals) = @_;
97 return undef unless 0 == grep { !defined } values %vals;
98 return join '|', ref $self || $self, $self->result_source->name,
99 map { $_ . '=' . $vals{$_} } sort keys %vals;
102 =head2 ident_condition
104 my $cond = $result_source->ident_condition();
106 my $cond = $result_source->ident_condition('alias');
108 Produces a condition hash to locate a row based on the primary key(s).
112 sub ident_condition {
113 my ($self, $alias) = @_;
115 my $prefix = defined $alias ? $alias.'.' : '';
116 $cond{$prefix.$_} = $self->get_column($_) for $self->primary_columns;
124 Matt S. Trout <mst@shadowcatsystems.co.uk>
128 You may distribute this code under the same terms as Perl itself.