Excerpts from Tim King's message of 2015-09-17 18:20:15 -0300: > Hi, all. > > I have lookup-table relationships of the following form: > > __PACKAGE__->belongs_to( > size => 'Schema::Result::SizeLookup', > { 'foreign.size_id' => 'self.size_id' }, > ); > > > Now, I know that I can create a new row like this (all made-up > pseudocode, so please forgive any typos), and have DBIC look up the > size_id from the SizeLookup table: > > $schema->resultset('TeeShirt')->create({ > serial_number => 'XYZ1234', > size => { name => 'large' }, > }); > > > But I would really like to be able to do something *similar* to this: > > $schema->resultset('TeeShirt')->create({ > serial_number => 'XYZ1234', > size_name => 'large', > }); > > $tee_shirt->update({ > size_name => 'small', > }); > > > We're a little surprised that this isn't a common desire, to be able to > create/update looked-up IDs based on the corresponding identifying > string. But we can't seem to find any off-the-shelf component that will > enable something like this. > > We did see DBIx::Class::LookupColumn, but that provides additional > methods, not column-like functionality. > > Any suggestions? > > Tim >
If you use Moose together with DBIx you can write something along these lines: around 'create' => sub { my $orig = shift; my $self = shift; my (%args) = @_; if ($args{size_name}) { $args{size} = { name => $args{size_name} }; } $self->$orig(%args); } That will provide a way to map your 'new way' to provide arguments to the 'create' function. You may also consider write a general trait for that, so you can use in other classes the same behavior for any <related_class>_<related_column> key contained in %args. For pure DBIx class (no Moose), you can see http://search.cpan.org/~ribasushi/DBIx-Class-0.082820/lib/DBIx/Class/Manual/Component.pod#CREATING_COMPONENTS Should be easy for this use case and a little more difficult for the general one. -- Marco Arthur @ (M)arco Creatives _______________________________________________ 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