DBIx::C...cDefault(3)
hiii bestie!! welcome to the DBIx::C...cDefault(3) page from the User Contributed Perl Documentation π this one is lowkey iconic if you want your database fields to update themselves, so let's get into it.
π NAME π
DBIx::Class::DynamicDefault β Automatically set and update fields β¨ that's literally the whole vibe.
β¨ SYNOPSIS β¨
okay so first, here's the setup bestie β look at this little beauty:
package My::Schema::SomeTable;
__PACKAGE__->load_components(qw/DynamicDefault ... Core/);
__PACKAGE__->add_columns(
quux => { data_type => 'integer' },
quux_plus_one => { data_type => 'integer',
dynamic_default_on_create => \&quux_plus_one_default,
dynamic_default_on_update => 'quux_plus_one_default', },
last_changed => { data_type => 'integer',
dynamic_default_on_create => 'now',
dynamic_default_on_update => 'now, },
);
sub quux_plus_one_default {
my ($self) = @_;
return $self->quux + 1;
}
sub now {
return DateTime->now->epoch;
}
Now, any update or create actions will set the specified columns to the value returned by the callback you specified as a method name or code reference. periodt π
πΈ DESCRIPTION πΈ
so the whole point is automatically set and update fields with values calculated at runtime π¦ it's giving "my columns are main characters and they update themselves" energy, no cap.
π OPTIONS π
bestie, these are the options and they're honestly kind of essential π
π¦ dynamic_default_on_create π¦
dynamic_default_on_create => sub { ... }
dynamic_default_on_create => 'method_name'
When inserting a new row, all columns with the dynamic_default_on_create option will be set to the return value of the specified callback β unless the column's value has been explicitly set. The callback gets invoked with the row object as its only argument, and it may be a code reference or a method name π
π¦ dynamic_default_on_update π¦
dynamic_default_on_update => sub { ... }
dynamic_default_on_update => 'method_name'
When updating a row, all columns with the dynamic_default_on_update option will be set to the return value of the specified callback β unless the column's value has been explicitly set.
Tiny catch: Columns will only be altered if other dirty columns exist. See always_update on how to change this π
π¦ always_update π¦
always_update => 1
When setting always_update to 1, dynamic_default_on_update callbacks will always be invoked, even if no other columns are dirty. obsessed. π₯
π AUTHOR π
This iconic human is Florian Ragwitz <rafl@debian.org> π
π LICENSE π
This software is copyright (c) 2008 by Florian Ragwitz.
This is free software; you can redistribute it and/or modify it under the same terms as perl itself.
mwah π β this was perl v5.42.0, dated 2017-10-16, on the DBIx::C...cDefault(3) page. bye bestie!! π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-DBIx-Class-DynamicDefault 0.04-bp160.1.4 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms