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 Returns the primary key(s) for a row. Can't be called as
32 $self->throw_exception( "Can't call id() as a class method" )
34 my @id_vals = $self->_ident_values;
35 return (wantarray ? @id_vals : $id_vals[0]);
39 my ($self, $use_storage_state) = @_;
43 for ($self->result_source->_pri_cols_or_die) {
44 push @ids, ($use_storage_state and exists $self->{_column_data_in_storage}{$_})
45 ? $self->{_column_data_in_storage}{$_}
46 : $self->get_column($_)
48 push @missing, $_ if (! defined $ids[-1] and ! $self->has_column_loaded ($_) );
51 if (@missing && $self->in_storage) {
52 $self->throw_exception (
53 'Unable to uniquely identify result object with missing PK columns: '
54 . join (', ', @missing )
63 Returns a unique id string identifying a result object by primary key.
64 Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
65 L<DBIx::Class::ObjectCache>.
71 The default C<_create_ID> method used by this function orders the returned
72 values by the alphabetical order of the primary column names, B<unlike>
73 the L</id> method, which follows the same order in which columns were fed
74 to L<DBIx::Class::ResultSource/set_primary_key>.
82 $self->throw_exception( "Can't call ID() as a class method" )
84 return undef unless $self->in_storage;
85 return $self->_create_ID(%{$self->ident_condition});
89 my ($self, %vals) = @_;
90 return undef unless 0 == grep { !defined } values %vals;
91 return join '|', ref $self || $self, $self->result_source->name,
92 map { $_ . '=' . $vals{$_} } sort keys %vals;
95 =head2 ident_condition
97 my $cond = $result_source->ident_condition();
99 my $cond = $result_source->ident_condition('alias');
101 Produces a condition hash to locate a row based on the primary key(s).
105 sub ident_condition {
106 shift->_mk_ident_cond(@_);
109 sub _storage_ident_condition {
110 shift->_mk_ident_cond(shift, 1);
114 my ($self, $alias, $use_storage_state) = @_;
116 my @pks = $self->result_source->_pri_cols_or_die;
117 my @vals = $self->_ident_values($use_storage_state);
120 my $prefix = defined $alias ? $alias.'.' : '';
122 if (! defined ($cond{$prefix.$col} = shift @vals) ) {
127 if (@undef && $self->in_storage) {
128 $self->throw_exception (
129 'Unable to construct result object identity condition due to NULL PK columns: '
130 . join (', ', @undef)
139 =head1 AUTHOR AND CONTRIBUTORS
141 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
145 You may distribute this code under the same terms as Perl itself.