Skip to content

Importing the name from a one to many relationship via a CSV file? #258

Open

Description

@adamstacey

Sorry, if this question has been asked and the answer is simple, but I have spent the last few hours researching the answer and have been unable to find it.

I have a simple product entity that has a name and a one to many relationship with a manufacturer entity.

I would like to try and import a CSV file that simply has a product name and manufacturer name as the column headers that then adds the products with the associated manufacturer.

I have tried using name and manufacturer and name and manufacturer.name, but it has not worked.

I have also tried using a StringToObjectConverter and used the manufacturer.id, but that did not work either.

Any help would be massively appreciated.

Thanks in advance.

Activity

  1. Baachi commented on Sep 2, 2015

    @Baachi
    Collaborator

    Hey sorry for the delay.

    I think the best solution would be to create your own converter which transform the array (which comes from the csv) to your product entity.

    Short example (so you get an idea what i mean).

    $converters = new ConverterStep();
    $converters->add(new ArrayValueConverterMap([
         'manufactor1' => new StringToObjectConverter($repo, 'manufactor1'),
         'manufactor2' => new StringToObjectConverter($repo, 'manufactor2'),
         'manufactor2' => new StringToObjectConverter($repo, 'manufactor3'),
    ]));
    $converters->add(function($item) {
         $product = new Product();
         $product->setSku($item['sku']);
         $product->addManufactor($item['manufactor1']);
         $product->addManufactor($item['manufactor2']);
         $product->addManufactor($item['manufactor3']);          
    
         return $product;
    });
    
    $workflow = new StepAggregator (new CSVReader('file.csv'));
    $workflow->addStep($converters);
    $workflow->process();
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions