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

lsolesen’s picture

Status: Active » Closed (cannot reproduce)

One should remember to enable the module.

Sivert’s picture

Hi. 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?

joeyda3rd’s picture

Status: Closed (cannot reproduce) » Active

(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:

// in __contruct()...
  $address_arguments= array(
      'thoroughfare' => array('source_field' => 'src_street_address'),
      'locality' => array('source_field' => 'city'),
      'administrative_area' => array('source_field'=>'state'),
      'postal_code' => array('source_field' => 'zipcode'),
  );

  $this->addFieldMapping('listing_address','src_country')
       ->arguments($address_arguments);

  $this->addFieldMapping(NULL, 'src_street_address');
  $this->addFieldMapping(NULL, 'streetnumber');
  $this->addFieldMapping(NULL, 'streetnumbermodifier');
  $this->addFieldMapping(NULL, 'streetdirection');
  $this->addFieldMapping(NULL, 'streetname');
  $this->addFieldMapping(NULL, 'streetsuffix');
  $this->addFieldMapping(NULL, 'streetpostdirection');
  $this->addFieldMapping(NULL, 'unit');
  $this->addFieldMapping(NULL, 'zipplus4');
  $this->addFieldMapping(NULL, 'state');


/*----------------------------------------------*/
//in prepareRow()...
  $row->src_street_address =
      $row->streetnumber.' '.
      $row->streetnumbermodifier.' '.
      $row->streetdirection.' '.
      $row->streetname.' '.
      $row->streetsuffix.' '.
      $row->streetpostdirection.' '.
      $row->unit; //should move unit to sub_premise

   $row->src_street_address = str_replace('  ',' ',trim($row->src_street_address));
   $row->src_street_address = str_replace('  ',' ',trim($row->src_street_address)); //gotta be a better way to do this...

   $this->src_country = "US";

   if(!empty($row->zipplus4) && isset($row->zipplus4))
     $row->zipcode .= $row->zipplus4;

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.

bastnic’s picture

@joeyda3rd, you should just change that line :

$this->src_country = "US";

to

$this->src_country = array("US");
lsolesen’s picture

Status: Active » Closed (works as designed)
joeyda3rd’s picture

Version: 7.x-2.2 » 7.x-2.3
Status: Closed (works as designed) » Active

I tried as recommended in the post above, but still not migrating addressfield instances.

rar’s picture

Did anyone actually get a working example? Can this be turned into a documentation request?

bastnic’s picture

@rar, yes :

// in __contruct()...
    $arguments = array(
      'first_name'    => array('source_field' => 'firstname'),
      'last_name'     => array('source_field' => 'lastname'),
      'thoroughfare'  => array('source_field' => 'adress'),
      // 'premise'    => array('source_field' => 'address2'),
      'locality'      => array('source_field' => 'town'),
      'postal_code'   => array('source_field' => 'pc')
    );
    // The country should be passed in as the primary value.
    $this->addFieldMapping('field_user_adress', 'country')
      ->arguments($arguments);


/*----------------------------------------------*/
//in prepareRow()...
    $row->country = array("FR");

Works for me.

rar’s picture

Finally 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.

class Wusel_Step1_UserMigration extends Wusel_Basic_Migration {
  public function __construct() {
    parent::__construct();
    $this->description = t('Import an CSV-file (only "Account"-fields)');
    $columns = array(
 ...
      3 => array('address1', t('Address line 1')),
      4 => array('address2', t('Address line 2')),
      5 => array('city', t('city')),
      6 => array('state', t('state')),
      7 => array('zip', t('zip')),
     );
 ...
    $address_arguments = array(
        'thoroughfare' => array('source_field' => 'address1'),
        'premise' => array('source_field' => 'address2'),
        'locality' => array('source_field' => 'city'),
        'administrative_area' => array('source_field'=>'state'),
        'postal_code' => array('source_field' => 'zip'),
    );
    //address mapped fields
    $this->addFieldMapping('field_user_address','src_country')
       ->arguments($address_arguments);
...   
   $this->addFieldMapping(NULL, 'address1');
    $this->addFieldMapping(NULL, 'address2');
    $this->addFieldMapping(NULL, 'city');
    $this->addFieldMapping(NULL, 'zip');
    $this->addFieldMapping(NULL, 'state');

...
 } //end public function __construct
 
  public function prepareRow($row)  {
    $row->src_country = array("US");
  } //end function prepareRow
} //end class Wusel_Step1_UserMigration extends Wusel_Basic_Migration
mikeryan’s picture

Status: Active » Fixed

Looks like things are working.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Fixed source code.