You are here

public function BeerNodeMigration::__construct in Migrate 6.2

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

General initialization of a Migration object.

Overrides BasicExampleMigration::__construct

File

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

Class

BeerNodeMigration
The BeerNodeMigration uses the migrate_example_beer_node table as source and creates Drupal nodes of type 'Beer' as destination.

Code

public function __construct() {
  parent::__construct();
  $this->description = t('Beers of the world');

  // You may optionally declare dependencies for your migration - other migrations
  // which should run first. In this case, terms assigned to our nodes and
  // the authors of the nodes should be migrated before the nodes themselves.
  $this->dependencies = array(
    'BeerTerm',
    'BeerUser',
  );
  $this->map = new MigrateSQLMap($this->machineName, array(
    'bid' => array(
      'type' => 'int',
      'not null' => TRUE,
      'description' => 'Beer ID.',
      'alias' => 'b',
    ),
  ), MigrateDestinationNode::getKeySchema());

  // We have a more complicated query. The Migration class fundamentally
  // depends on taking a single source row and turning it into a single
  // Drupal object, so how do we deal with zero or more terms attached to
  // each node? One way (demonstrated for MySQL) is to pull them into a single
  // comma-separated list.
  $query = db_select('migrate_example_beer_node', 'b')
    ->fields('b', array(
    'bid',
    'name',
    'body',
    'excerpt',
    'aid',
    'countries',
    'image',
    'image_alt',
    'image_title',
    'image_description',
  ));
  $query
    ->leftJoin('migrate_example_beer_topic_node', 'tb', 'b.bid = tb.bid');

  // Gives a single comma-separated list of related terms
  $query
    ->groupBy('tb.bid');
  $query
    ->addExpression('GROUP_CONCAT(tb.style)', 'terms');

  // By default, MigrateSourceSQL derives a count query from the main query -
  // but we can override it if we know a simpler way
  $count_query = db_select('migrate_example_beer_node', 'b');
  $count_query
    ->addExpression('COUNT(bid)', 'cnt');

  // Passing the cache_counts option means the source count (shown in
  // drush migrate-status) will be cached - this can be very handy when
  // dealing with a slow source database.
  $this->source = new MigrateSourceSQL($query, array(), $count_query, array(
    'cache_counts' => TRUE,
  ));

  // Set up our destination - nodes of type migrate_example_beer
  $this->destination = new MigrateDestinationNode('migrate_example_beer');

  // Mapped fields
  $this
    ->addFieldMapping('title', 'name')
    ->description(t('Mapping beer name in source to node title'));
  $this
    ->addFieldMapping('sticky')
    ->description(t('Should we default this to 0 or 1?'))
    ->issueGroup(t('Client questions'))
    ->issueNumber(765736)
    ->issuePriority(MigrateFieldMapping::ISSUE_PRIORITY_LOW);

  // References to related objects (such as the author of the content) are
  // most likely going to be identifiers from the source data, not Drupal
  // identifiers (such as uids). You can use the mapping from the relevant
  // migration to translate from the old ID to the Drupal identifier.
  // Note that we also provide a default value of 1 - if the lookup fails to
  // find a corresponding uid for the aid, the owner will be the administrative
  // account.
  $this
    ->addFieldMapping('uid', 'aid')
    ->sourceMigration('BeerUser')
    ->defaultValue(1);

  // This is a multi-value text field
  $this
    ->addFieldMapping('field_migrate_example_country', 'countries')
    ->separator('|');

  // These are related terms, which by default will be looked up by name
  $this
    ->addFieldMapping('Migrate Example Beer Styles', 'terms')
    ->separator(',');
  $this
    ->addFieldMapping('body', 'body');
  $this
    ->addFieldMapping('teaser', 'excerpt');

  // Copy an image file, write DB record to files table, and save in Field storage.
  // Note how we specify the source query fields that will map to the file
  // alt/title/description values.
  $arguments = MigrateFileFieldHandler::arguments(drupal_get_path('module', 'migrate_example'));
  $this
    ->addFieldMapping('field_migrate_example_image', 'image')
    ->arguments($arguments);
  $this
    ->addFieldMapping('field_migrate_example_image:alt', 'image_alt');
  $this
    ->addFieldMapping('field_migrate_example_image:title', 'image_title');
  $this
    ->addFieldMapping('field_migrate_example_image:description', 'image_description');

  // No unmapped source fields
  // Unmapped destination fields
  $this
    ->addUnmigratedDestinations(array(
    'name',
    'created',
    'changed',
    'status',
    'promote',
    'revision',
    'language',
  ));
}