Search code examples
pimcore

Pimcore 6 CSV Importing


Pimcore newbie here. I'm struggling a bit with the csv import procedure, the docs are a bit shallow and I couldn't find a more comprehensive developer reference.

  • I'm not getting how Resolver Settings work. What's the meaning of setting a strategy? (ID throws Pimcore\DataObject\Import\Resolver\ImportErrorException: Could not resolve object with id. Filename runs, but then it seems that I don't have control on how objects get labeled)

  • I have two date columns in my imported csv, so I'd like to fill my object with those. Still, the dates are written like "2016-05-30T14:36:03" so I guess that I need to process a bit to transform the string in a format that could be recognized by pimcore. Do I need to do this in Column Configuration (by converting the specific column as an Operator PHP Code. BTW, do I need to create a class for every column, or I can group them by data format?) or more globally by setting the Resolver Strategy as Code and editing all the row properties at once?


Solution

  • About the resolver

    When importing a CSV, Pimcore has for each row basically two options: create a new DataObject or update an existing one. When you want Pimcore to update existing DataObjects then you need some logic to retrieve that specific DataObject based on some info in the CSV. That's where the Resolver comes in place.

    Pimcore supports 5 different resolver strategies:

    • Id: your CSV needs to contain a column with the DataObject id, and that value is used to fetch the corresponding DataObject to update
    • Filename: same as above, but for the filename (= key) of the DataObject
    • Fullpath: same as above, but for the full path of the DataObject
    • GetByAttribute: same as above, but for a given attribute within the DataObject (for example if you have a field ProductCode in it)
    • Code: a custom PHP class containing logic to retrieve the existing DataObject

    The Id operator has one drawback, which you are currently running into:

    Resolves the objects via the object ID. The object has to exist, otherwise an error is thrown.

    Based on your question, I have the feeling that you are only inserting new DataObjects (at least for now, maybe you might also want to update later on).

    When creating DataObjects you need to specify a key/filename (most likely you already noticed that when creating an object manually), so your CSV should already (if it doesn't you should add it) contain a column with either that key/filename or with the full path for the new DataObjects. That same column can/should be used to resolve any (potentially already) existing DataObjects.

    So you should use either the Filename or Fullpath Resolver. Both have options what to do if the object already exists (update it? ignore it?) and (more important in your situation) what to do when it doesn't exist yet.

    About the dates

    Looking at the code, any string that can be parsed by the PHP's method strtotime can be entered in your CSV. Your value 2016-05-30T14:36:03 parses just fine.