Perl DBIx :: Class - default values ​​when using new ()?

When using the new () method for a DBIx :: Class ResultSource data source to create a (potentially temporary) variable, it does not seem to populate the attributes with the default values ​​specified in the DBIC schema (which we specified to create a table from this schema).

We are currently creating one default value for one such class (the first time this was a problem) with

sub new {
  my $class = shift;
  my $self = $class->next::method(@_);
  $self->queue('DEFAULT_QUEUE_VAL') unless $self->queue();
  return $self;
}

in this class (i.e., in the attribute queue => DEFAULT_QUEUE_VAL). However, in the long run, we have several DBIC classes that have different default values, and we would like to avoid repeating the above logic for all the different cases.

Are there CPAN modules / plugins for this? We did not see in our (admittedly, cursory) CPAN search.

Edit: fixed some garbage in the sample code; it turns out i cp'd from outdated code.

+5
source share
3 answers

It looks like there is no DBIC component for this, you can do it with a little fancy code with your existing code:

sub new {
  my $class = shift;
  my $self = $class->next::method(@_);
  foreach my $col ($self->result_source->columns) {
    my $default = $self->result_source->column_info($col)->{default_value};
    $self->$col($default) if($default && !defined $self->$col());
  return $self;
}

Since this is straightforward, there is not much for the component.

+2
source

is it not your turn to call code () as a class method, not an object method? you meant

$new->queue('DEFAULT_QUEUE_VAL') unless $new->queue();

?

Edit - sorry, just re-read the question and assume it's just a typo

- SQL, ? NULL (undef), , new() db (- > discard_changes() ?)

0

"" , , . , .

, DATETIME , .

0

All Articles