I've made a migration which should create field collections whose host_entity is a field collection created by prior migration. I read in the main F_C migration issue where AlexWeber was able do this (he even posts code) but when I run my migration I get "Could not find host entity of the field collection to import."

Due to my source data, my maps have to be multi-values. The parent field collection, called RateAnalysis uses a map of account_ID and rate_plan_id. The child field collection, BillPeriod, uses account_ID, rate_plan_id, and bill_period_start_date. So my BillPeriod migration is as follows:

class BillPeriodMigration extends BasicMigration {
  public function __construct() {
    parent::__construct();
    $this->description = t('Import bill period data from CSV file.');

    // Create a map object for tracking the relationships between source rows
    $this->map = new MigrateSQLMap($this->machineName,
        array(
          'account_ID' => array('type' => 'varchar',
                           'length' => 12,
                           'not null' => TRUE,
                           'description' => 'account_ID',
                          ),
          'rate_plan_id' => array('type' => 'varchar',
                           'length' => 255,
                           'not null' => TRUE,
                           'description' => 'rate_plan_id',
                          ),
          'bill_period_start_date' => array('type' => 'varchar',
                           'length' => 255,
                           'not null' => TRUE,
                           'description' => 'bill_period_start_date',
                          ),
        ),
        MigrateDestinationNode::getKeySchema()
      );
    
    $this->destination = new MigrateDestinationFieldCollection(
      'field_bill_period', 
      array('host_entity_type' => 'field_collection_item')
	);

    $this->addFieldMapping('host_entity_id', 'constructed_key')->sourceMigration('RateAnalysis');
    //field mappings here....
  }

  function prepareRow($row) {
	if (parent::prepareRow($row) === FALSE) {
	  return FALSE;
	}
  	// Don't import total rows
  	if($row->record_type == "TOTAL"){
	    return FALSE;
    }
    // Important: key fields must be in the same order as in RateAnalysis' MigrateSQLMap constructor
    $row->constructed_key = array($row->account_ID, $row->rate_plan_id);
  }
}

When I look at the map for RateAnalysis via phpMyAdmin the values for sourceID1 and SourceID2 match exactly the array values that are generated in my prepareRow function (constructed_key).

When I trace the import method (field_collection.migrate.inc) it gets a parameter called $collection which should have the host_entity_id but it's coming up as an empty array thus the error. But I can't grok were $collection is set up.

If anyone can help me figure out where I've gone wrong I'd be very grateful.

Comments

jmuzz’s picture

Status: Active » Fixed

From destination.inc in the migrate module:

  /**
   * Derived classes must implement import(), to construct one new object (pre-pppulated
   * using field mappings in the Migration). It is expected to call prepare and
   * complete handlers, passing them $row (the raw data from the source).
   */
  abstract public function import(stdClass $object, stdClass $row);

So you need to set up a field mapping for host_entity_id from your source data.

Hope this helps.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.