0

Has anyone tried migrating fields with multiple source data rows? In my case, Im migrating values of commerce file field (https://www.drupal.org/project/commerce_file). It only migrated 1 id where it should have multiple id's. I've checked the query in the source database, it outputs correctly, my problem is upon migration.

Here's what I did.

$query->addExpression('GROUP_CONCAT(file.field_file_fid)', 'file_id');

/* … */

$this->addFieldMapping('commerce_file', 'file_id')
        ->sourceMigration('PrivateFile')
        ->separator(',');
$this->addFieldMapping('commerce_file:file_class')
        ->defaultValue('MigrateFileFid');
$this->addFieldMapping('commerce_file:preserve_files')
        ->defaultValue(1);
1

I found out that sourceMigration() doesn't allow multiple data source data rows (but please correct me if I'm wrong). So, I made a custom query in my prepareRow() that somehow works like sourceMigration. Here's what I did:


// group concat code
$query->addExpression('GROUP_CONCAT(file.field_file_fid)', 'file_id');

// in field mapping
$this->addFieldMapping('commerce_file', 'file_id');
$this->addFieldMapping('commerce_file:file_class')
      ->defaultValue('MigrateFileFid');

// in prepareRow()
public function prepareRow($row) {
  if (parent::prepareRow($row) === FALSE) {
    return FALSE;
  }

  // get the values of the file_id where you used group concat function
  $product_id1 = explode(",", $row->file_id); //convert csv to array
  $product_id1 = array_values($product_id1); //redeclare it to clear its index / optional
  $newsIds = array(); //array where the ids will be stored

  foreach ($product_id1 as $i) { //loop through array
      $getEquivalentId = db_select('migrate_map_privatefile', 'p') //'migrate_map_privatefile' is the migrate map table where the ids of the files are stored
              ->fields('p', array('destid1')) 
              ->condition('p.sourceid1', $i);
      $resultOfId = $getEquivalentId->execute();
      foreach ($resultOfId as $id) {
        $newsIds[] = $id->destid1; //store result to $newsIds
      }

    }

    $row->file_id = $newsIds; //the values of file_id will now be $newsIds

  }

Hope this would help someone, someday. Cheers!

0

To migrate multiple values in destination database, we just need to provide source values in array. Migrate module will automatically parse this array and migrate accordingly.

See this : https://www.drupal.org/node/1159234

As per your code: You can prepare file id array in prepareRow() method of migrate module.

$row->file_id_array = array(fid1, fid2);

And in __construct() you can do field mapping like below:

$this->addFieldMapping('file_field', 'file_id_array');
    $this->addFieldMapping('file_field:file_class')
      ->defaultValue('MigrateFileUri');
    $this->addFieldMapping('file_field:destination_file', 'DESTINATION_FILENAMEs');
    $this->addFieldMapping('file_field:source_dir')
      ->defaultValue($file_path);
    $this->addFieldMapping('file_field:destination_dir')
      ->defaultValue(variable_get('file_default_scheme', 'public') . '://event');
  • thanks for the answer @mohit_rocks. However, I'm using source migration. And I just found out that source migration doesn't support multiple source data rows? Was that right? – japhfortin Nov 16 '15 at 9:32
  • So, I think my problem now would be how to create and prepare a query that works like sourceMigration() - (gets the migrated ids of the files). – japhfortin Nov 16 '15 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.