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]);
43 for ($self->_pri_cols) {
44 push @ids, $self->get_column($_);
45 push @missing, $_ if (! defined $ids[-1] and ! $self->has_column_loaded ($_) );
48 if (@missing && $self->in_storage) {
49 $self->throw_exception (
50 'Unable to uniquely identify row object with missing PK columns: '
51 . join (', ', @missing )
60 Returns a unique id string identifying a row object by primary key.
61 Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
62 L<DBIx::Class::ObjectCache>.
68 The default C<_create_ID> method used by this function orders the returned
69 values by the alphabetical order of the primary column names, B<unlike>
70 the L</id> method, which follows the same order in which columns were fed
71 to L<DBIx::Class::ResultSource/set_primary_key>.
79 $self->throw_exception( "Can't call ID() as a class method" )
81 return undef unless $self->in_storage;
82 return $self->_create_ID(%{$self->ident_condition});
86 my ($self, %vals) = @_;
87 return undef unless 0 == grep { !defined } values %vals;
88 return join '|', ref $self || $self, $self->result_source->name,
89 map { $_ . '=' . $vals{$_} } sort keys %vals;
92 =head2 ident_condition
94 my $cond = $result_source->ident_condition();
96 my $cond = $result_source->ident_condition('alias');
98 Produces a condition hash to locate a row based on the primary key(s).
102 sub ident_condition {
103 my ($self, $alias) = @_;
105 my @pks = $self->_pri_cols;
106 my @vals = $self->_ident_values;
109 my $prefix = defined $alias ? $alias.'.' : '';
111 if (! defined ($cond{$prefix.$col} = shift @vals) ) {
116 if (@undef && $self->in_storage) {
117 $self->throw_exception (
118 'Unable to construct row object identity condition due to NULL PK columns: '
119 . join (', ', @undef)
130 Matt S. Trout <mst@shadowcatsystems.co.uk>
134 You may distribute this code under the same terms as Perl itself.