How to Import xPath fields of XML to Pimcore DataDirector dataport raw data fields?

69 Views Asked by At

Pimcore has powerfull plugin DataDirector for import automation. It proposes manual configuration of fields to import and their mapping to Pimcore fields https://www.youtube.com/watch?v=nyhKJTzTq-4&list=PL4-QRNfdsdKIfzQIP-c9hRruXf0r48fjt

It works fine if you have 10-50 fields. How to import that configuration from some csv file whan you have 700+ fields?

1

There are 1 best solutions below

0
On

There is no ability to import using interface of commandline DataDirector API.

I tried to request such functionality creation from vendor -- it costs.

I tried to edit it in MySQL but it is strictly coupled to other data, see: SELECT sourceconfig FROM plugin_pim_dataport;

Solution:

Each dataport can be exported and imported to JSON. This is our chance.

  1. create sample dataport using your source XML

  2. export it to sample.json

  3. unserialize $json_a['plugin_pim_dataport']['sourceconfig'] and $json_a['plugin_pim_rawitemField'] containers

         $string = file_get_contents(
             "sample.json"
         );
         $json_a = json_decode($string, true);
         $sourceConfig = $json_a['plugin_pim_dataport']['sourceconfig'];
         $sourceConfig = unserialize($sourceConfig, ['allowed_classes' => false]);
    
         $fieldsConfig = $json_a['plugin_pim_rawitemField']; 
    
  4. add new fields to them from any source e.g. CSV

         while (($data = fgetcsv($handle, 1000, ",")) !== false) {
             $row++;
             $id = $max + 1 + $row;
             $data = $this->addField($id, $data[0], $data[1]);
             $sourceConfig['fields']['field_' . ($id + 1)] = $data['sourceConfig'];
             $fieldsConfig[$id] = $data['fieldsConfig'];
         }
    
     public function addField(
         $id,
         $name = 'ObjectBrick/Field',
         $xpath = '//*[.//MainContainer = "systeme"]//*[.//Description = "SomeAttribute"]/anyPath/SampleContainer'
     ) {
         $res = [];
    
         $res['fieldsConfig'] = [
             "dataportId" => "1",
             "fieldNo"    => $id + 1,
             "name"       => $name,
             "priority"   => $id
         ];
         $res['sourceConfig'] = [
             'xpath'       => $xpath,
             'multiValues' => false,
         ];
    
         return $res;
     }
    
  5. serialize, save to JSON, import to your dataport.