[Dbix-class] Dynamic defaults

Benjamin Martin benmartin at venda.com
Tue Nov 29 15:27:15 GMT 2011


thanks for the response. :)

I guess I should expand a little.

I use DBIx::Class::Schema::Loader to build my DBIx::Class classes. 
Mainly because the schema is subject to extensive and constant change 
(nightmare!) and this was a good way to ensure my orm is up to date with 
the schema)

I use Moose to apply a Role to my Result class, so (for example) at the 
end of the Section.pm I have:

use Moose;
with 'My::Schema::Roles::Result::Section';

and within the Role, I have the code that does (amongst other things) 
the 'around new' from my prev. post.


On 29/11/11 15:05, Frank Schwach wrote:
> 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
>
>




More information about the DBIx-Class mailing list