1 package DBIx::Class::PK;
6 use base qw/DBIx::Class::Row/;
8 use DBIx::Class::_Util 'fail_on_internal_call';
13 DBIx::Class::PK - Primary Key class
19 This class contains methods for handling primary keys and methods
28 Returns the primary key(s) for a row. Can't be called as
33 sub id :DBIC_method_is_indirect_sugar {
34 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
36 $_[0]->throw_exception( "Can't call id() as a class method" )
40 ? $_[0]->_ident_values
41 : ($_[0]->_ident_values)[0] # FIXME - horrible horrible legacy crap
46 my ($self, $use_storage_state) = @_;
50 for ($self->result_source->_pri_cols_or_die) {
51 push @ids, ($use_storage_state and exists $self->{_column_data_in_storage}{$_})
52 ? $self->{_column_data_in_storage}{$_}
53 : $self->get_column($_)
55 push @missing, $_ if (! defined $ids[-1] and ! $self->has_column_loaded ($_) );
58 if (@missing && $self->in_storage) {
59 $self->throw_exception (
60 'Unable to uniquely identify result object with missing PK columns: '
61 . join (', ', @missing )
70 Returns a unique id string identifying a result object by primary key.
71 Used by L<DBIx::Class::CDBICompat::LiveObjectIndex> and
72 L<DBIx::Class::ObjectCache>.
78 The default C<_create_ID> method used by this function orders the returned
79 values by the alphabetical order of the primary column names, B<unlike>
80 the L</id> method, which follows the same order in which columns were fed
81 to L<DBIx::Class::ResultSource/set_primary_key>.
89 $self->throw_exception( "Can't call ID() as a class method" )
91 return undef unless $self->in_storage;
92 return $self->_create_ID(%{$self->ident_condition});
96 my ($self, %vals) = @_;
97 return undef if 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 shift->_mk_ident_cond(@_);
116 sub _storage_ident_condition {
117 shift->_mk_ident_cond(shift, 1);
121 my ($self, $alias, $use_storage_state) = @_;
123 my @pks = $self->result_source->_pri_cols_or_die;
124 my @vals = $self->_ident_values($use_storage_state);
127 my $prefix = defined $alias ? $alias.'.' : '';
129 if (! defined ($cond{$prefix.$col} = shift @vals) ) {
134 if (@undef && $self->in_storage) {
135 $self->throw_exception (
136 'Unable to construct result object identity condition due to NULL PK columns: '
137 . join (', ', @undef)
144 =head1 FURTHER QUESTIONS?
146 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
148 =head1 COPYRIGHT AND LICENSE
150 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
151 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
152 redistribute it and/or modify it under the same terms as the
153 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.