Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adamstacey opened this issue Sep 1, 2015 · 1 comment
Open

Comments

@adamstacey
Copy link

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.

@Baachi
Copy link
Collaborator

Baachi commented Sep 2, 2015

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants