You are here

public function BeerUserMigration::__construct in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 migrate_example/beer.inc \BeerUserMigration::__construct()

General initialization of a Migration object.

Overrides BasicExampleMigration::__construct

File

migrate_example/beer.inc, line 214
A basic example of using the Migrate module to import taxonomy, users, nodes, and comments.

Class

BeerUserMigration
And that's it for the BeerTerm migration! For a simple migration, all you have to do is define the source, the destination, and mappings between the two - to import the data you simply do: drush migrate-import BeerTerm

Code

public function __construct($arguments) {

  // The basic setup is similar to BeerTermMigraiton
  parent::__construct($arguments);
  $this->description = t('Beer Drinkers of the world');
  $query = db_select('migrate_example_beer_account', 'mea')
    ->fields('mea', array(
    'aid',
    'status',
    'posted',
    'name',
    'nickname',
    'password',
    'mail',
    'sex',
    'beers',
  ));
  $this->source = new MigrateSourceSQL($query);
  $this->destination = new MigrateDestinationUser();
  $this->map = new MigrateSQLMap($this->machineName, array(
    'aid' => array(
      'type' => 'int',
      'not null' => TRUE,
      'description' => 'Account ID.',
    ),
  ), MigrateDestinationUser::getKeySchema());

  // One good way to organize your mappings in the constructor is in three
  // groups - mapped fields, unmapped source fields, and unmapped destination
  // fields.
  // Mapped fields
  // This is a shortcut you can use when the source and destination field
  // names are identical (e.g., the email address field is named 'mail' in
  // both the source table and in Drupal).
  $this
    ->addSimpleMappings(array(
    'status',
    'mail',
  ));

  // Our source table has two entries for 'alice', but we must have a unique
  // username in the Drupal 'users' table. dedupe() creates new, unique
  // destination values when the source field of that value already exists.
  // For example, if we're importing a user with name 'test' and a user
  // 'test' already exists in the target, we'll create a new user named
  // 'test_1'.
  // dedupe() takes the Drupal table and column for determining uniqueness.
  $this
    ->addFieldMapping('name', 'name')
    ->dedupe('users', 'name');

  // The migrate module automatically converts date/time strings to UNIX
  // timestamps.
  $this
    ->addFieldMapping('created', 'posted');
  $this
    ->addFieldMapping('pass', 'password');

  // Instead of mapping a source field to a destination field, you can
  // hardcode a default value. You can also use both together - if a default
  // value is provided in addition to a source field, the default value will
  // be applied to any rows where the source field is empty or NULL.
  $this
    ->addFieldMapping('roles')
    ->defaultValue(DRUPAL_AUTHENTICATED_RID);
  $this
    ->addFieldMapping('field_migrate_example_gender', 'sex');

  // The source field has beer names separated by a pipe character ('|'). By
  // adding ->separator('|'), the migration will automatically break them out,
  // look up the node with each title, and assign the node reference to this
  // user.
  if (module_exists('node_reference')) {
    $this
      ->addFieldMapping('field_migrate_example_favbeers', 'beers')
      ->separator('|');
  }
  else {
    $this
      ->addFieldMapping(NULL, 'beers')
      ->issueGroup(t('DNM'));
  }

  // Unmapped source fields
  $this
    ->addFieldMapping(NULL, 'nickname')
    ->issueGroup(t('DNM'));

  // Unmapped destination fields
  // This is a shortcut you can use to mark several destination fields as DNM
  // at once.
  $this
    ->addUnmigratedDestinations(array(
    'access',
    'data',
    'is_new',
    'language',
    'login',
    'picture',
    'role_names',
    'signature',
    'signature_format',
    'theme',
    'timezone',
  ));

  // Oops, we made a typo - this should have been 'init'! If you have
  // migrate_ui enabled, look at the BeerUser info page - you'll see that it
  // displays a warning "int used as destination field in mapping but not in
  // list of destination fields", and also lists "1 unmapped" under Destination,
  // where it highlights "init" as unmapped.
  $this
    ->addFieldMapping('int')
    ->issueGroup(t('DNM'));
  $destination_fields = $this->destination
    ->fields();
  if (isset($destination_fields['path'])) {
    $this
      ->addFieldMapping('path')
      ->issueGroup(t('DNM'));
    if (isset($destination_fields['pathauto'])) {
      $this
        ->addFieldMapping('pathauto')
        ->issueGroup(t('DNM'));
    }
  }
}