I am attempting to use the patch in comment #222 of this issue (https://drupal.org/node/1175082#comment-7625869) to migrate data into a multi-value field collection.
I create an array of the objects in prepareRow() but when the migration completes and I review the nodes, only the first item in the array has populated the field collection and no subsequent items have been added. Am I missing something important?
<?php
class IngredientsMigration extends BasicMigration {
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Migrate recipe ingredients from the source database into field collections.');
$this->dependencies = array('Recipe');
$query = Database::getConnection('default', 'legacy')
->select('recipes', 'r')
->fields('r', array('id', 'ingredients'))
->condition('r.id', array(2), 'IN');
$this->source = new MigrateSourceSQL($query);
$this->destination = new MigrateDestinationFieldCollection(
'field_collection_ingredients',
array('host_entity_type' => 'node')
);
$this->map = new MigrateSQLMap($this->machineName,
array(
'id' => array('type' => 'int',
'length' => 11,
'not null' => TRUE,
'description' => 'Recipe ID',
'alias' => 'r',
),
),
MigrateDestinationFieldCollection::getKeySchema()
);
$this->addFieldMapping('host_entity_id', 'id')
->sourceMigration('Recipe')
->issueNumber(16);
$this->addFieldMapping('field_ingredient_text', 'ingredients')
->description(t('See prepareRow method'))
->issueNumber(16);
$this->addUnmigratedDestinations(array(
'path',
)
);
}
public function prepareRow($row) {
// Always include this fragment at the beginning of every prepareRow()
// implementation, so parent classes can ignore rows.
if (parent::prepareRow($row) === FALSE) {
return FALSE;
}
$ingredients_array = preg_split('/$\R?^/m', $row->ingredients);
$row->ingredients = array();
foreach ($ingredients_array as $ingredient) {
$row->ingredients[] = $ingredient;
}
return TRUE;
}
}