[Dbix-class] create failures with has_one

Wallace Reis wallace at reis.org.br
Tue Oct 20 01:19:34 GMT 2009


On 19/10/2009, at 11:59, Dermot wrote:
> 2009/10/19 Wallace Reis <wallace at reis.org.br>:
>> On 15/10/2009, at 21:08, Dermot wrote:
>>>>>>> my $record = {
>>>>>>>       'validation_pass' => 1,
>>>>>>>       'files' => [],
>>>>>>>       'active' => 1,
>>>>>>>       'name' => 'Gustav MR',
>>>>>>>       'clipdata' => [
>>>>>>>                       {
>>>>>>>                         'contrib_id' => 2,
>>>>>>>                       }
>>>>>>>                     ],
>>>>>>>       'contrib_id' => 2,
>>>>>>>     };
>>>>>>>
>>>>>>
>>>>>> Use a HashRef for clipdata rel instead of a ArrayRef.
>
> That might explain why, even under 0.08112, I get
> "DBIx::Class::ResultSet::find_or_create(): new_result needs a hash..."
> when I use a nested structure. Are there any methods I can use that
> will allow me to create new, nested rows?

You can encapsulate it in a resultset method like:

sub import_record {
     my ( $self, $record ) = @_;
     my ( $files, $clipdata ) = map { delete $record->{$_} } qw/files  
clipdata/;
     my $schema = $self->result_source->schema;

     my $imp_rec;
     my $import_txn = sub {
         my $imported_record = $self->update_or_create($record);
         foreach my $file (@$files) {
             $imported_record->update_or_create_related('files', $file,
                 { key => 'files_filename' }
             );
         }
         $imported_record->update_or_create_related('clipdata',  
$clipdata);
         return $imported_record;
     };
     eval { $imp_rec = $schema->txn_do($import_txn) };
     if ($@) {                                      # Transaction failed
         die "something terrible has happened!"     #
             if ($@ =~ /Rollback failed/);          # Rollback failed

         # deal_with_failed_transaction();
     }
     return $imp_rec;
}

then call $schema->resultset('Submissions')->import_record($record).

--
    wallace reis/wreis         Catalyst and DBIx::Class consultancy  
with a clue
    Software Engineer          and a commit bit: http://shadowcat.co.uk/catalyst/
Shadowcat Systems Limited
http://www.shadowcat.co.uk     http://www.linkedin.com/in/wallacereis



More information about the DBIx-Class mailing list