Re: [Catalyst] No database defaults with FormHandler and DBIC
Hi Gerda, sorry for late reply. Didn't have time to look into this. Now I have: my $init_row = $c->model('DB::Lesson')->new_result({}); $validated = $self->formDetail->process(item => $init_row); And I still don't see the database defaults in the rendered form. Any clue where to look now? Thank you -- Martin On 2016.11.01 12:22:15 -0400, Gerda Shank wrote: > A database row is used for defaults only if it is provided in the ‘item’ > attribute. If you provide only the item_id and item_class they are used to > update the row but not for defaults. > > Gerda > > > On Mon, Oct 31, 2016 at 12:00 PM, Martin Rehakwrote: > > > Hi, > > > > thanks much for comments. > > > > I know there are number of ways how/where to set/define defaults from app > > code > > and there is large text written about it in Catalyst documentation. > > > > What I would expect to work at least is that database default is going to > > be > > propagated into the form from the schema definition. I don't need to have > > the > > second place with explicit default definition when there is and implicit > > definition coming from db. > > > > In other words: > > $ script/reha_create.pl model DB DBIC::Schema reha::Schema create=static > > 'dbi:Pg:dbname=;host=' reha > > > > generates > > > > =head2 capacity > > > > data_type: 'integer' > > default_value: 1 > > is_nullable: 1 > > > > and there is the default definition successfully fetched from db. How to > > get it > > used without any override (and duplication) in the app code? > > > > Thank you very much in advance. > > > > Regards > > -- > > Martin > > > > On 2016.10.31 09:23:26 +, LNATION . wrote: > > > Probs incorrect approach but it'll work > > > > > > before render => sub { > > > unless ($_[0]->field('capacity')->value) { # mayb editing > > > $_[0]->field('capacity')->value(1); > > > } > > > } > > > > > > On Mon, Oct 31, 2016 at 8:28 AM, Marc0 > > wrote: > > > > > > > Am 31.10.2016 um 09:15 schrieb Marc0: > > > > > Am 31.10.2016 um 08:57 schrieb Martin Rehak: > > > > >> has_field 'capacity' => (type => 'PosInteger', > > > > >> #default => '1', > > > > >> label => 'Kapacita'); > > > > > > > > > > maybe add a "lazy => 1" to your commented "default => '1'"? > > > > > > > > > > has_field q(capacity) => ( > > > > > type=> q(PosInteger), > > > > > lazy=> 1, > > > > > default => q(1), > > > > > label => q(Kapacita), > > > > > ); > > > > > > > > maybe "capacity" is explicitely set to undef, you could try an "around" > > > > method modifier to check for a defined value: > > > > > > > > has_field q(capacity) => ( > > > > is => q(ro), # just guessing > > > > type=> q(PosInteger), > > > > lazy=> 1, > > > > default => q(1), > > > > label => q(Kapacita), > > > > writer => q(_set_default_capacity), > > > > ); > > > > > > > > # untested, but according to Moose::Manual::MethodModifiers > > > > around q(has_field) => sub { > > > > my $orig = shift; > > > > my $self = shift; > > > > > > > > # check for definedness and set to default value if > > > > # not defined (NULL) > > > > $self->_set_default_capacity(1) > > > > unless defined $self->$orig(); > > > > > > > > return $self->$orig(); > > > > }; > > > > > > > > Regards > > > > -- > > > > Marc0 > > > > > > > > ___ > > > > List: Catalyst@lists.scsys.co.uk > > > > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst > > > > Searchable archive: http://www.mail-archive.com/ > > > > catalyst@lists.scsys.co.uk/ > > > > Dev site: http://dev.catalyst.perl.org/ > > > > > > > > > ___ > > > List: Catalyst@lists.scsys.co.uk > > > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst > > > Searchable archive: http://www.mail-archive.com/ > > catalyst@lists.scsys.co.uk/ > > > Dev site: http://dev.catalyst.perl.org/ > > > > > > ___ > > List: Catalyst@lists.scsys.co.uk > > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst > > Searchable archive: http://www.mail-archive.com/ > > catalyst@lists.scsys.co.uk/ > > Dev site: http://dev.catalyst.perl.org/ > > > ___ > List: Catalyst@lists.scsys.co.uk > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst > Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ > Dev site: http://dev.catalyst.perl.org/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] No database defaults with FormHandler and DBIC
Also form definition looks like this: has 'formDetail' => (isa => 'reha::Form::LessonDetail', is => 'rw', lazy => 1, default => sub {reha::Form::LessonDetail->new }); Is that right in case I would like to have database defaults used after process()? Regards -- M. On 2016.12.10 15:36:10 +0100, Martin Řehák wrote: > Hi Gerda, > > sorry for late reply. Didn't have time to look into this. > > Now I have: > > my $init_row = $c->model('DB::Lesson')->new_result({}); > $validated = $self->formDetail->process(item => $init_row); > > And I still don't see the database defaults in the rendered form. Any clue > where to look now? > > Thank you > -- > Martin > > On 2016.11.01 12:22:15 -0400, Gerda Shank wrote: > > A database row is used for defaults only if it is provided in the ‘item’ > > attribute. If you provide only the item_id and item_class they are used to > > update the row but not for defaults. > > > > Gerda > > > > > > On Mon, Oct 31, 2016 at 12:00 PM, Martin Rehak <l...@tekkirk.org> wrote: > > > > > Hi, > > > > > > thanks much for comments. > > > > > > I know there are number of ways how/where to set/define defaults from app > > > code > > > and there is large text written about it in Catalyst documentation. > > > > > > What I would expect to work at least is that database default is going to > > > be > > > propagated into the form from the schema definition. I don't need to have > > > the > > > second place with explicit default definition when there is and implicit > > > definition coming from db. > > > > > > In other words: > > > $ script/reha_create.pl model DB DBIC::Schema reha::Schema create=static > > > 'dbi:Pg:dbname=;host=' reha > > > > > > generates > > > > > > =head2 capacity > > > > > > data_type: 'integer' > > > default_value: 1 > > > is_nullable: 1 > > > > > > and there is the default definition successfully fetched from db. How to > > > get it > > > used without any override (and duplication) in the app code? > > > > > > Thank you very much in advance. > > > > > > Regards > > > -- > > > Martin > > > > > > On 2016.10.31 09:23:26 +, LNATION . wrote: > > > > Probs incorrect approach but it'll work > > > > > > > > before render => sub { > > > > unless ($_[0]->field('capacity')->value) { # mayb editing > > > > $_[0]->field('capacity')->value(1); > > > > } > > > > } > > > > > > > > On Mon, Oct 31, 2016 at 8:28 AM, Marc0 <catal...@hidden-primary.net> > > > wrote: > > > > > > > > > Am 31.10.2016 um 09:15 schrieb Marc0: > > > > > > Am 31.10.2016 um 08:57 schrieb Martin Rehak: > > > > > >> has_field 'capacity' => (type => 'PosInteger', > > > > > >> #default => '1', > > > > > >> label => 'Kapacita'); > > > > > > > > > > > > maybe add a "lazy => 1" to your commented "default => '1'"? > > > > > > > > > > > > has_field q(capacity) => ( > > > > > > type=> q(PosInteger), > > > > > > lazy=> 1, > > > > > > default => q(1), > > > > > > label => q(Kapacita), > > > > > > ); > > > > > > > > > > maybe "capacity" is explicitely set to undef, you could try an > > > > > "around" > > > > > method modifier to check for a defined value: > > > > > > > > > > has_field q(capacity) => ( > > > > > is => q(ro), # just guessing > > > > > type=> q(PosInteger), > > > > > lazy=> 1, > > > > > default => q(1), > > > > > label => q(Kapacita), > > > > > writer => q(_set_default_capacity), > > > > > ); > > > > > > > > > > # untested, but according to Moose::Manual::MethodModifiers > > > > > around q(has_field) => sub { > > > > > my $orig = shift; > > > > > my $self = shift; > > > > > > > > &
Re: [Catalyst] No database defaults with FormHandler and DBIC
Hi, thank you for kickstarting me. I still don't understand there is no such method provided by DBIx already I have resolved the issue with this diff: diff -r d65de14e366f lib/reha/Controller/Lesson.pm --- a/lib/reha/Controller/Lesson.pm Sat Dec 10 17:22:07 2016 +0100 +++ b/lib/reha/Controller/Lesson.pm Sun Dec 11 19:35:28 2016 +0100 @@ -54,17 +54,9 @@ my ($self, $c, $lesson_id) = @_; my ($validated); - my $init_row = $c->model('DB::Lesson')->new_result({}); + my $row = $c->model('DB::Lesson')->find_or_default({id => $lesson_id}); $validated = $self->formDetail->process( - item => $init_row, + item => $row, params => $c->req->parameters); if ($validated) { diff -r d65de14e366f lib/reha/Schema.pm --- a/lib/reha/Schema.pmSat Dec 10 17:22:07 2016 +0100 +++ b/lib/reha/Schema.pmSun Dec 11 19:35:28 2016 +0100 @@ -8,7 +8,9 @@ use MooseX::MarkAsMethods autoclean => 1; extends 'DBIx::Class::Schema'; -__PACKAGE__->load_namespaces; +__PACKAGE__->load_namespaces( + default_resultset_class => '+reha::Schema::ResultSet::Base', + resultset_namespace => 'Base'); # Created by DBIx::Class::Schema::Loader v0.07043 @ 2016-02-05 16:52:32 diff -r d65de14e366f lib/reha/Schema/ResultSet/Base.pm --- /dev/null Thu Jan 01 00:00:00 1970 + +++ b/lib/reha/Schema/ResultSet/Base.pm Sun Dec 11 19:35:28 2016 +0100 @@ -0,0 +1,28 @@ +package reha::Schema::ResultSet::Base; + +use strict; +use warnings; + +use base 'DBIx::Class::ResultSet'; + +sub find_or_default { + my $self = shift; + my $attrs= (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}); + my $hash = ref $_[0] eq 'HASH' ? shift : {@_}; + my $row; + + # return data if found + if (keys %$hash and $row = $self->find($hash, $attrs) ) { + return $row; + } + + # return new result with defaults prefilled + $row = $self->new_result($hash); + foreach my $col ($row->result_source->columns) { + my $default = $row->result_source->column_info($col)->{default_value}; + $row->$col($default) if($default && !defined $row->$col()); + } + return $row; +} + +1; Thank you very much all for patience with my issue. Regards -- Martin On 2016.12.10 18:00:05 +0100, LNATION . wrote: > Sorry Long day :) corrected sentence... > > The problem is because when you call ->new_result the default_value does > not get set, which is reasonable behavior the moment you are dealing with > time. I am unsure whether there is another create method which uses > database or even the schema column spec. > > Regards > > Robert > > On Sat, Dec 10, 2016 at 5:48 PM, LNATION . <thisusedtobeanem...@gmail.com> > wrote: > > > The problem I think is when you call >new_result the default value from > > your database does get **populated that happens when on insert > > > > On Sat, Dec 10, 2016 at 5:17 PM, Martin Řehák <re...@tekkirk.org> wrote: > > > >> Hi, > >> > >> I understand that there is a complicated solution. I am looking for > >> the simplest way. > >> > >> Doc says: > >> > >> > >> For forms where you pass in an 'item' (usually a database row object), > >> the values in that object will be used preferentially; if an accessor > >> exists in the 'item' object, then the defaults won't be used. (If an > >> accessor doesn't exist, the defaults *will* be used.) > >> > >> $form->process( item => $row, params => {} ); > >> > >> For the above call the 'default' on the field will not be used, which is > >> usually what you want. > >> > >> > >> What is the easiest way how to get defaults going from the database > >> schema into a form, please? Is process() function able to propagate > >> defaults from $row into $form? How to achieve that? > >> > >> Regards > >> -- > >> Martin > >> > >> On 2016.12.10 16:51:52 +0100, LNATION . wrote: > >> > My email coding has some syntax errors, apologies. > >> > > >> > On Sat, Dec 10, 2016 at 4:50 PM, LNATION . < > >> thisusedtobeanem...@gmail.com> > >> > wrote: > >> > > >> > > and then make a role role > >> > > > >> > > has default_column_spec => ( ...) > >> > > > >> > > before render => sub { > >> > > while (my ($field, $default) = each %{ > >> $_[0]->default_column_spec}{ > >> > >