[Dbix-class] Dynamic defaults

Frank Schwach fs5 at sanger.ac.uk
Tue Nov 29 15:05:23 GMT 2011


DBIx::Class is not using Moose


On Tue, 2011-11-29 at 14:31 +0000, Benjamin Martin wrote:
> hello,
> 
> I am not sure if I am doing the right thing so I ask here for advice 
> please :)
> 
> I have 2 tables, chart and sections. A chart is made of multiple 
> sections (chart has_many sections)
> When I add a new section to a chart I want to give it a default name of 
> 'Section 1' or 'Section 2' and so on.
> 
> The DBIx::Class docs says that to provide defaults one should have a 
> 'new' method.
> The Moose docs state to "never override new"
> 
> The above makes me think I am doing the wrong thing. In my Section class 
> I have this:
> 
> 
> around new => sub {
>      my ($orig, $self) = (shift, shift);
>      my ( $attrs ) = @_;
> 
>      if ( $attrs->{chart_id} ) {
>          my $schema = $attrs->{-result_source}->schema;
>          my $chart = $schema->resultset('Chart')->find( 
> $attrs->{chart_id} );
>          unless( defined $attrs->{name} ) {
>              my $number_of_sections = $chart->sections->count;
>              $attrs->{name} = 'Section ' . ($number_of_sections + 1);
>          }
>      }
> 
>      my $new = $self->$orig($attrs);
>      return $new;
> };
> 
> 
> Is there a better way to achieve this?
> It seems some what hacky to use '-result_source' ... is there a better way?
> 
> Thanks for any advice you can give.
> 
> tar,
> -b
> 
> 
> 
> 
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk



-- 
 The Wellcome Trust Sanger Institute is operated by Genome Research 
 Limited, a charity registered in England with number 1021457 and a 
 company registered in England with number 2742969, whose registered 
 office is 215 Euston Road, London, NW1 2BE. 



More information about the DBIx-Class mailing list