You are here

class BeerTermMigration in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 migrate_example/beer.inc \BeerTermMigration

There are four essential components to set up in your constructor: $this->source - An instance of a class derived from MigrateSource, this will feed data to the migration. $this->destination - An instance of a class derived from MigrateDestination, this will receive data that originated from the source and has been mapped by the Migration class, and create Drupal objects. $this->map - An instance of a class derived from MigrateMap, this will keep track of which source items have been imported and what destination objects they map to. Mappings - Use $this->addFieldMapping to tell the Migration class what source fields correspond to what destination fields, and additional information associated with the mappings.

Hierarchy

Expanded class hierarchy of BeerTermMigration

1 string reference to 'BeerTermMigration'
migrate_example_migrate_api in migrate_example/migrate_example.module

File

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

View source
class BeerTermMigration extends BasicExampleMigration {
  public function __construct() {
    parent::__construct();

    // Human-friendly description of your migration process. Be as detailed as you
    // like.
    $this->description = t('Migrate styles from the source database to taxonomy terms');

    // Create a map object for tracking the relationships between source rows
    // and their resulting Drupal objects. Usually, you'll use the MigrateSQLMap
    // class, which uses database tables for tracking. Pass the machine name
    // (BeerTerm) of this migration to use in generating map and message tables.
    // And, pass schema definitions for the primary keys of the source and
    // destination - we need to be explicit for our source, but the destination
    // class knows its schema already.
    $this->map = new MigrateSQLMap($this->machineName, array(
      'style' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Topic ID',
      ),
    ), MigrateDestinationTerm::getKeySchema());

    // In this example, we're using tables that have been added to the existing
    // Drupal database but which are not Drupal tables. You can examine the
    // various tables (starting here with migrate_example_beer_topic) using a
    // database browser like phpMyAdmin.
    // First, we set up a query for this data. Note that by ordering on
    // style_parent, we guarantee root terms are migrated first, so the
    // parent_name mapping below will find that the parent exists.
    $query = db_select('migrate_example_beer_topic', 'met')
      ->fields('met', array(
      'style',
      'details',
      'style_parent',
      'region',
      'hoppiness',
    ))
      ->orderBy('style_parent', 'ASC');

    // Create a MigrateSource object, which manages retrieving the input data.
    $this->source = new MigrateSourceSQL($query);

    // Set up our destination - terms in the migrate_example_beer_styles vocabulary
    $this->destination = new MigrateDestinationTerm('Migrate Example Beer Styles');

    // Assign mappings TO destination fields FROM source fields. To discover
    // the names used in these calls, use the drush commands
    // drush migrate-fields-destination BeerTerm
    // drush migrate-fields-source BeerTerm
    $this
      ->addFieldMapping('name', 'style');
    $this
      ->addFieldMapping('description', 'details');

    // Documenting your mappings makes it easier for the whole team to see
    // exactly what the status is when developing a migration process.
    $this
      ->addFieldMapping('parent_name', 'style_parent')
      ->description(t('The incoming style_parent field is the name of the term parent'));

    // Mappings are assigned issue groups, by which they are grouped on the
    // migration info page when the migrate_ui module is enabled. The default
    // is 'Done', indicating active mappings which need no attention. A
    // suggested practice is to use groups of:
    // Do Not Migrate (or DNM) to indicate source fields which are not being used,
    //  or destination fields not to be populated by migration.
    // Client Issues to indicate input from the client is needed to determine
    //  how a given field is to be migrated.
    // Implementor Issues to indicate that the client has provided all the
    //  necessary information, and now the implementor needs to complete the work.
    $this
      ->addFieldMapping(NULL, 'hoppiness')
      ->description(t('This info will not be maintained in Drupal'))
      ->issueGroup(t('DNM'));

    // Open mapping issues can be assigned priorities (the default is
    // MigrateFieldMapping::ISSUE_PRIORITY_OK). If you're using an issue
    // tracking system, and have defined issuePattern (see BasicExampleMigration
    // above), you can specify a ticket/issue number in the system on the
    // mapping and migrate_ui will link directory to it.
    $this
      ->addFieldMapping(NULL, 'region')
      ->description('Will a field be added to the vocabulary for this?')
      ->issueGroup(t('Client Issues'))
      ->issuePriority(MigrateFieldMapping::ISSUE_PRIORITY_MEDIUM)
      ->issueNumber(770064);

    // It is good practice to account for all source and destination fields
    // explicitly - this makes sure that everyone understands exactly what is
    // being migrated and what is not. Also, migrate_ui highlights unmapped
    // fields, or mappings involving fields not in the source and destination,
    // so if (for example) a new field is added to the destination field it's
    // immediately visible, and you can find out if anything needs to be
    // migrated into it.
    $this
      ->addFieldMapping('format')
      ->issueGroup(t('DNM'));
    $this
      ->addFieldMapping('weight')
      ->issueGroup(t('DNM'));
    $this
      ->addFieldMapping('parent')
      ->issueGroup(t('DNM'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BeerTermMigration::__construct public function General initialization of a Migration object. Overrides BasicExampleMigration::__construct
DynamicMigration::isDynamic public static function Overrides default of FALSE Overrides MigrationBase::isDynamic
Migration::$counts protected property An array of counts. Initially used for cache hit/miss tracking.
Migration::$currentSourceKey protected property
Migration::$defaultRollbackAction protected property The default rollback action for this migration. Can be overridden on a per-row basis by setting $row->rollbackAction in prepareRow().
Migration::$destination protected property Destination object for the migration, derived from MigrateDestination.
Migration::$destinationValues protected property The object currently being constructed
Migration::$fieldMappings protected property Simple mappings between destination fields (keys) and source fields (values).
Migration::$highwaterField protected property If present, an array with keys name and alias (optional). Name refers to the source columns used for tracking highwater marks. alias is an optional table alias.
Migration::$map protected property Map object tracking relationships between source and destination data
Migration::$needsUpdate public property Specify value of needs_update for current map row. Usually set by MigrateFieldHandler implementations.
Migration::$rollbackAction public property The rollback action to be saved for the current row.
Migration::$rollbackBatchSize protected property When performing a bulkRollback(), the maximum number of items to pass in a single call. Can be overridden in derived class constructor.
Migration::$source protected property Source object for the migration, derived from MigrateSource.
Migration::$sourceValues protected property The current data row retrieved from the source.
Migration::$systemOfRecord protected property
Migration::addFieldMapping public function Add a mapping for a destination field, specifying a source field and/or a default value. 1
Migration::addSimpleMappings public function Shortcut for adding several fields which have the same name on both source and destination sides.
Migration::addUnmigratedDestinations public function Shortcut for adding several destination fields which are to be explicitly not migrated.
Migration::addUnmigratedSources public function Shortcut for adding several source fields which are to be explicitly not migrated.
Migration::analyze public function Perform an analysis operation - report on field values in the source.
Migration::applyMappings protected function Apply field mappings to a data row received from the source, returning a populated destination object. 1
Migration::beginProcess protected function Override MigrationBase::beginProcess, to make sure the map/message tables are present. Overrides MigrationBase::beginProcess
Migration::checkStatus protected function Standard top-of-loop stuff, common between rollback and import - check for exceptional conditions, and display feedback.
Migration::createStubWrapper protected function If stub creation is enabled, try to create a stub and save the mapping.
Migration::deregisterMigration public static function Deregister a migration - remove all traces of it from the database (without touching any content which was created by this migration). Overrides MigrationBase::deregisterMigration
Migration::DESTINATION constant
Migration::endProcess public function Override MigrationBase::endProcess, to call post hooks. Note that it must be public to be callable as the shutdown function. Overrides MigrationBase::endProcess
Migration::errorCount public function Get the number of source records which failed to import. TODO: Doesn't yet account for informationals, or multiple errors for a source record.
Migration::getDestination public function
Migration::getFieldMappings public function
Migration::getHighwaterField public function
Migration::getMap public function
Migration::getSource public function
Migration::getSystemOfRecord public function
Migration::handleDedupe protected function For fields which require uniqueness, assign a new unique value if necessary.
Migration::handleSourceMigration protected function Look up a value migrated in another migration.
Migration::import protected function Perform an import operation - migrate items from source to destination.
Migration::importedCount public function Get the number of records successfully imported.
Migration::isComplete public function Reports whether this migration process is complete (i.e., all available source rows have been processed). Overrides MigrationBase::isComplete
Migration::itemOptionExceeded protected function Test whether we've exceeded the designated item limit.
Migration::messageCount public function Get the number of messages associated with this migration
Migration::postImport protected function
Migration::postRollback protected function
Migration::preImport protected function Default implementations of pre/post import/rollback methods. These call the destination methods (if they exist) - when overriding, always call parent::preImport() etc.
Migration::prepareKey public function Default implementation of prepareKey. This method is called from the source plugin immediately after retrieving the raw data from the source - by default, it simply assigns the key values based on the field names passed to MigrateSQLMap(). Override…
Migration::prepareRow public function Default implementation of prepareRow(). This method is called from the source plugin upon first pulling the raw data from the source. 1
Migration::prepareUpdate public function Prepares this migration to run as an update - that is, in addition to unmigrated content (source records not in the map table) being imported, previously-migrated content will also be updated in place.
Migration::preRollback protected function
Migration::processedCount public function Get the number of source records processed.
Migration::progressMessage protected function Outputs a progress message, reflecting the current status of a migration process.
Migration::removeFieldMapping public function Remove any existing mappings for a given destination or source field.
Migration::rollback protected function Perform a rollback operation - remove migrated items from the destination.
Migration::saveMessage public function Pass messages through to the map class Overrides MigrationBase::saveMessage
Migration::setUpdate public function Set the specified row to be updated, if it exists.
Migration::SOURCE constant Indicate whether the primary system of record for this migration is the source, or the destination (Drupal). In the source case, migration of an existing object will completely replace the Drupal object with data from the source side. In the…
Migration::sourceCount public function Convenience function to return count of total source records
Migration::updateCount public function Get the number of records marked as needing update.
MigrationBase::$currentMigration protected static property Track the migration currently running, so handlers can easily determine it without having to pass a Migration object everywhere.
MigrationBase::$dependencies protected property List of other Migration classes which should be imported before this one. E.g., a comment migration class would typically have node and user migrations as dependencies.
MigrationBase::$description protected property Detailed information describing the migration.
MigrationBase::$displayFunction protected static property Name of a function for displaying feedback. It must take the message to display as its first argument, and a (string) message type as its second argument (see drush_log()).
MigrationBase::$enabled protected property Disabling a migration prevents it from running with --all, or individually without --force
MigrationBase::$group protected property The name of a migration group, used to collect related migrations.
MigrationBase::$issuePattern protected property If provided, an URL for an issue tracking system containing :id where the issue number will go (e.g., 'http://example.com/project/ticket/:id').
MigrationBase::$logHistory protected property Whether to maintain a history of migration processes in migrate_log
MigrationBase::$logID protected property Primary key of the current history record (inserted at the beginning of a process, to be updated at the end)
MigrationBase::$machineName protected property The machine name of this Migration object, derived by removing the 'Migration' suffix from the class name. Used to construct default map/message table names, displayed in drush migrate-status, key to migrate_status table...
MigrationBase::$memoryLimit protected property The PHP memory_limit expressed in bytes.
MigrationBase::$memoryThreshold protected property The fraction of the memory limit at which an operation will be interrupted. Can be overridden by a Migration subclass if one would like to push the envelope. Defaults to 85%.
MigrationBase::$migrations protected static property
MigrationBase::$options protected property Save options passed to current operation
MigrationBase::$previousErrorHandler protected property If we set an error handler (during import), remember the previous one so it can be restored.
MigrationBase::$processing protected property Indicates that we are processing a rollback or import - used to avoid excess writes in endProcess()
MigrationBase::$starttime protected property When the current operation started.
MigrationBase::$status protected property Are we importing, rolling back, or doing nothing?
MigrationBase::$team protected property MigrateTeamMember objects representing people involved with this migration.
MigrationBase::$timeLimit protected property The PHP max_execution_time.
MigrationBase::$timeThreshold protected property The fraction of the time limit at which an operation will be interrupted. Can be overridden by a Migration subclass if one would like to push the envelope. Defaults to 90%.
MigrationBase::$total_processed protected property Number of "items" processed in the current migration process (whatever that means for the type of process)
MigrationBase::currentMigration public static function
MigrationBase::dependenciesComplete protected function Reports whether all (hard) dependencies have completed migration
MigrationBase::displayMessage public static function Output the given message appropriately (drush_print/drupal_set_message/etc.)
MigrationBase::errorHandler public function Custom PHP error handler. TODO: Redundant with hook_watchdog?
MigrationBase::generateMachineName protected function By default, the migration machine name is the class name (with the Migration suffix, if present, stripped).
MigrationBase::getDependencies public function
MigrationBase::getDescription public function
MigrationBase::getEnabled public function
MigrationBase::getGroup public function
MigrationBase::getHardDependencies public function
MigrationBase::getHighwater public function Fetch the current highwater mark for updated content.
MigrationBase::getInstance public static function Return the single instance of the given migration.
MigrationBase::getIssuePattern public function
MigrationBase::getItemLimit public function
MigrationBase::getLastImported public function Retrieve the last time an import operation completed successfully.
MigrationBase::getLastThroughput public function Retrieve the last throughput for current Migration (items / minute).
MigrationBase::getMachineName public function
MigrationBase::getMessageLevelName public function Get human readable name for a message constant.
MigrationBase::getOption public function
MigrationBase::getSoftDependencies public function
MigrationBase::getStatus public function Check the current status of a migration.
MigrationBase::getTeam public function
MigrationBase::getTimeLimit public function
MigrationBase::handleException public function Takes an Exception object and both saves and displays it, pulling additional information on the location triggering the exception.
MigrationBase::incompleteDependencies public function Returns an array of the migration's dependencies that are incomplete.
MigrationBase::loggingCallback public function Database logging callback - called when there's a database error. We log non-critical stuff, and throw an exception otherwise TODO: Eliminate in favor of hook_watchdog()?
MigrationBase::machineFromClass protected static function
MigrationBase::memoryExceeded protected function Test whether we've exceeded the desired memory threshold. If so, output a message.
MigrationBase::MESSAGE_ERROR constant Message types to be passed to saveMessage() and saved in message tables. MESSAGE_INFORMATIONAL represents a condition that did not prevent the operation from succeeding - all others represent different severities of conditions resulting in a source…
MigrationBase::MESSAGE_INFORMATIONAL constant
MigrationBase::MESSAGE_NOTICE constant
MigrationBase::MESSAGE_WARNING constant
MigrationBase::processImport public function Perform an operation during the import phase
MigrationBase::processRollback public function Perform an operation during the rollback phase.
MigrationBase::registerMigration public static function Register a new migration process in the migrate_status table. This will generally be used in two contexts - by the class detection code for static (one instance per class) migrations, and by the module implementing dynamic (parameterized class)…
MigrationBase::resetMigrations public static function Clear the cached list of migration objects.
MigrationBase::resetStatus public function Reset the status of the migration to IDLE (to be used when the status gets stuck, e.g. if a process core-dumped)
MigrationBase::RESULT_COMPLETED constant Codes representing the result of a rollback or import process.
MigrationBase::RESULT_DISABLED constant
MigrationBase::RESULT_FAILED constant
MigrationBase::RESULT_INCOMPLETE constant
MigrationBase::RESULT_SKIPPED constant
MigrationBase::RESULT_STOPPED constant
MigrationBase::saveHighwater protected function Save the highwater mark for this migration (but not when using an idlist).
MigrationBase::setDisplayFunction public static function
MigrationBase::staticInitialize public static function Initialize static members, before any class instances are created.
MigrationBase::STATUS_DISABLED constant
MigrationBase::STATUS_IDLE constant Codes representing the current status of a migration, and stored in the migrate_status table.
MigrationBase::STATUS_IMPORTING constant
MigrationBase::STATUS_ROLLING_BACK constant
MigrationBase::STATUS_STOPPING constant
MigrationBase::stopProcess public function Signal that any current import or rollback process should end itself at the earliest opportunity
MigrationBase::timeExceeded protected function Test whether we're approaching the PHP time limit.
MigrationBase::timeOptionExceeded protected function Test whether we've exceeded the designated time limit.
MigrationBase::timestamp public static function Convert an incoming string (which may be a UNIX timestamp, or an arbitrarily-formatted date/time string) to a UNIX timestamp.