Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 621248de

Von Sven Schöling vor mehr als 8 Jahren hinzugefügt

  • ID 621248dee40d10dfad3a464d332c2a879f68bd85
  • Vorgänger c74b01dd
  • Nachfolger f74b0dac

Csv: Doku-Update vor mapping feature

Unterschiede anzeigen:

SL/Helper/Csv.pm
488 488
  [ [ 'datatype', 'ordernumber', 'customer', 'transdate' ],
489 489
    [ 'datatype', 'partnumber', 'qty', 'sellprice' ] ]
490 490

  
491
=item C<profile> [{profile => \%ACCESSORS, class => class, row_ident => ri},]
491
=item C<profile> PROFILE_DATA
492 492

  
493
This is an ARRAYREF to HASHREFs which may contain the keys C<profile>, C<class>
494
and C<row_ident>.
493
The profile mapping csv to the objects.
494

  
495
See section L</PROFILE> for information on this topic.
496

  
497
=item C<ignore_unknown_columns>
498

  
499
If set, the import will ignore unkown header columns. Useful for lazy imports,
500
but deactivated by default.
501

  
502
=item C<case_insensitive_header>
503

  
504
If set, header columns will be matched against profile entries case
505
insensitive, and on match the profile name will be taken.
506

  
507
Only works if a profile is given, will die otherwise.
508

  
509
If both C<case_insensitive_header> and C<strict_profile> is set, matched header
510
columns will be accepted.
511

  
512
=item C<strict_profile>
513

  
514
If set, all columns to be parsed must be specified in C<profile>. Every header
515
field not listed there will be treated like an unknown column.
516

  
517
If both C<case_insensitive_header> and C<strict_profile> is set, matched header
518
columns will be accepted.
519

  
520
=back
521

  
522
=head1 PROFILE
523

  
524
The profile is needed for mapping csv data to the accessors in the data object.
525

  
526
The basic structure is:
527

  
528
  PROFILE       := [ CLASS_PROFILE, CLASS_PROFILE* ]
529
  CLASS_PROFILE := {
530
                      profile   => { ACCESSORS },
531
                      class     => $classname,
532
                      row_ident => $row_ident,
533
                   }
534
  ACCESSORS     := $field => $accessor, ACCESSORS*
495 535

  
496 536
The C<profile> is a HASHREF which may be used to map header fields to custom
497 537
accessors. Example:
498 538

  
499
  [ {profile => { listprice => listprice_as_number }} ]
539
  [
540
    {
541
      profile => {
542
        listprice => 'listprice_as_number',
543
      }
544
    }
545
  ]
500 546

  
501
In this case C<listprice_as_number> will be used to read in values from the
547
In this case C<listprice_as_number> will be used to store the values from the
502 548
C<listprice> column.
503 549

  
504 550
In case of a One-To-One relationship these can also be set over
505 551
relationships by separating the steps with a dot (C<.>). This will work:
506 552

  
507
  [ {profile => { customer => 'customer.name' }} ]
553
  customer => 'customer.name',
508 554

  
509 555
And will result in something like this:
510 556

  
511 557
  $obj->customer($obj->meta->relationship('customer')->class->new);
512 558
  $obj->customer->name($csv_line->{customer})
513 559

  
514
But beware, this will not try to look up anything in the database. You will
560
Beware, this will not try to look up anything in the database! You will
515 561
simply receive objects that represent what the profile defined. If some of
516
these information are unique, and should be connected to preexisting data, you
562
these information are unique, or should be connected to preexisting data, you
517 563
will have to do that for yourself. Since you provided the profile, it is
518 564
assumed you know what to do in this case.
519 565

  
520 566
If no profile is given, any header field found will be taken as is.
521 567

  
522 568
If the path in a profile entry is empty, the field will be subjected to
523
C<strict_profile> and C<case_insensitive_header> checking, will be parsed into
524
C<get_data>, but will not be attempted to be dispatched into objects.
569
C<strict_profile> and C<case_insensitive_header> checking and will be parsed
570
into C<get_data>, but will not be attempted to be dispatched into objects.
525 571

  
526
If C<class> is present, the line will be handed to the new sub of this class,
527
and the return value used instead of the line itself.
572
C<class> must be present. A new instance will be created for each line before
573
dispatching into it.
528 574

  
529
C<row_ident> is a string to recognize the right profile and class for each data
530
line in multiplexed data. It must match the value in the column 'dataype' for
531
each class.
575
C<row_ident> is used to determine the correct profile in multiplexed data and
576
must be given there. It's not used in non-multiplexed data.
532 577

  
533
In case of multiplexed data, C<class> and C<row_ident> must be given.
534 578
Example:
535
  [ {
579
  [
580
    {
536 581
      class     => 'SL::DB::Order',
537 582
      row_ident => 'O'
538 583
    },
539 584
    {
540 585
      class     => 'SL::DB::OrderItem',
541 586
      row_ident => 'I',
542
      profile   => {sellprice => sellprice_as_number}
543
    } ]
544

  
545
=item C<ignore_unknown_columns>
546

  
547
If set, the import will ignore unkown header columns. Useful for lazy imports,
548
but deactivated by default.
549

  
550
=item C<case_insensitive_header>
551

  
552
If set, header columns will be matched against profile entries case
553
insensitive, and on match the profile name will be taken.
554

  
555
Only works if a profile is given, will die otherwise.
556

  
557
If both C<case_insensitive_header> and C<strict_profile> is set, matched header
558
columns will be accepted.
559

  
560
=item C<strict_profile>
561

  
562
If set, all columns to be parsed must be specified in C<profile>. Every header
563
field not listed there will be treated like an unknown column.
564

  
565
If both C<case_insensitive_header> and C<strict_profile> is set, matched header
566
columns will be accepted.
567

  
568
=back
587
      profile   => { sellprice => 'sellprice_as_number' }
588
    },
589
  ]
569 590

  
570 591
=head1 ERROR HANDLING
571 592

  

Auch abrufbar als: Unified diff