Closed (fixed)
Project:
Migrate Extras
Version:
7.x-2.3
Component:
Migrate Extras Features
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
29 Oct 2011 at 18:50 UTC
Updated:
17 Aug 2012 at 23:11 UTC
I tried the following, but the data for the addressfield is not being saved. Am I doing anything wrong?
class UsersMigration extends BasicUsersMigration
{
function __construct()
{
parent::__construct();
//$this->dependencies = array('UserTerm');
$this->description = t('Migrate users from intraface');
$this->map = new MigrateSQLMap($this->machineName,
array('number' => array(
'type' => 'int',
'not null' => TRUE,
'description' => 'User ID.'
),
),
MigrateDestinationTerm::getKeySchema()
);
$this->addFieldMapping('field_aargang', 'keywords')
->separator(',');
$query = db_select('contact', 'contact'); //bring in a table $query
$query->innerJoin('address', 'address', 'contact.id = address.belong_to_id'); //and join in a table
$query
->fields('contact', array('number', 'code', 'date_created')) //get some fields
->fields('address', array('name', 'address', 'postcode', 'city', 'country', 'email', 'phone')) //get some fields
->condition('contact.active', '1') //add a condition - the third argument defaults to '='
// ->condition('address.type', 1) //add a condition - the third argument defaults to '='
->condition('address.active', '1') //add a condition - the third argument defaults to '='
->condition('address.email', '', '<>'); //add a condition - the third argument defaults to '='
$query->orderBy('address.name', 'ASC');
$this->source = new MigrateSourceSQL($query);
$this->destination = new MigrateDestinationUser(); // Drupal user
// Required fields that we will map
$this->addFieldMapping('pass', 'code');
$this->addFieldMapping('mail', 'email')
->defaultValue('no-email@invalid-email.dk');
$this->addFieldMapping('name', 'number');
$this->addFieldMapping('created', 'date_created');
$this->addFieldMapping('status')
->defaultValue('active');
// Adress fields to be added
$arguments = array(
'name_line' => array('source_field' => 'name'),
'thoroughfare' => array('source_field' => 'address'),
'locality' => array('source_field' => 'city'),
'postal_code' => array('source_field' => 'postcode'),
);
$this->addFieldMapping('field_address', 'country')->arguments($arguments)
->description('Address field');
// Custom fields to be mapped
//$this->addFieldMapping('field_phone', 'phone');
// Addressfield - Since the excerpt is mapped via an argument, add a null mapping so it's
// not flagged as unmapped.
$this->addFieldMapping(NULL, 'address');
$this->addFieldMapping(NULL, 'city');
$this->addFieldMapping(NULL, 'postcode');
// These fields will not be mapped in destination
$this->addFieldMapping('last_login_date')
->issueGroup(t('DNM'));
$this->addFieldMapping('access')
->issueGroup(t('DNM'));
$this->addFieldMapping('signature')
->issueGroup(t('DNM'));
$this->addFieldMapping('init')
->issueGroup(t('DNM'));
$this->addFieldMapping('picture')
->issueGroup(t('DNM'));
$this->addFieldMapping('language')
->issueGroup(t('DNM'));
$this->addFieldMapping('timezone')
->issueGroup(t('DNM'));
$this->addFieldMapping('signature_format')
->issueGroup(t('DNM'));
$this->addFieldMapping('theme')
->issueGroup(t('DNM'));
$this->addFieldMapping('roles')
->issueGroup(t('DNM'));
}
function prepareRow($row)
{
$row->date_created = strtotime($row->date_created);
return TRUE;
}
}
Comments
Comment #1
lsolesen commentedOne should remember to enable the module.
Comment #2
Sivert commentedHi. Does that mean that this code works? I am trying to do something similar, namely the following:
$addr_arguments = array(
'country' => array('source_field' => NULL),
'administrative_area' => array('source_field' => NULL),
'sub_administrative_area' => array('source_field' => NULL),
'dependent_locality' => array('source_field' => NULL),
'postal_code' => array('source_field' => NULL),
'premise' => array('source_field' => NULL),
'sub_premise' => array('source_field' => NULL),
'organisation_name' => array('source_field' => NULL),
'name_line' => array('source_field' => NULL),
'first_name' => array('source_field' => NULL),
'last_name' => array('source_field' => NULL),
'data' => array('source_field' => NULL),
'locality' => array('source_field' => 'city'),
'thoroughfare' => array('source_field' => 'street'),
);
$this->addFieldMapping('field_address_field', 'street')->arguments($addr_arguments);
but my migration just inserts the street value into the country field, the first of several columns in the field_address_field, causing an error (Data too long for column 'field_address_field_country' at row 1). Must certain modules be enabled for the splitting up to work?
Comment #3
joeyda3rd commented(I hope it's ok I reopened the issue)
I'm having the same problem, but I highly doubt it's anything other than an error in my code:
Any thoughts?
I'm pulling what little hair I have left on this one. I know that the sources contain values on migration because I use them elsewhere.
Comment #4
bastnic commented@joeyda3rd, you should just change that line :
to
Comment #5
lsolesen commentedComment #6
joeyda3rd commentedI tried as recommended in the post above, but still not migrating addressfield instances.
Comment #7
rar commentedDid anyone actually get a working example? Can this be turned into a documentation request?
Comment #8
bastnic commented@rar, yes :
Works for me.
Comment #9
rar commentedFinally got it, now I understand what's going on here. The object $row needs one extra key->value pair as 'XXX' = Array('US'), where XXX is defined with the addFieldMapping. XXX is the key for the array with only one argument (country) which is added to $row.
error_log(print_r($row),TRUE)let me know that prepareRow($row) was not working. (user error). I've posted working code with curly brackets.Comment #10
mikeryanLooks like things are working.
Comment #11.0
(not verified) commentedFixed source code.