0

I'm working on a D6-> D7 user migration where I need the source to be a node, not a user. To get around the restriction of only one author per node, in the source D6 site we created a Source content type, and then in other node types (article, video, etc), we created field_source as a node reference field to reference Source nodes.

So now, in my user migration to D7, I need to get the data from the Source nodes instead of users. Here's what I have so far.

/**
 * Override of DrupalUser6Migration.
 */
class CNSUserMigration extends DrupalUser6Migration {
  public function __construct($arguments) {
    parent::__construct($arguments);

    $this->addFieldMapping('name', 'title');
    // Set a default password for all users.
    $this->addFieldMapping('pass')->defaultValue('Pass!234');
    $this->addFieldMapping('field_twitter', 'field_twitter_id_value')->description('Twitter ID');
    $this->addFieldMapping('field_job_title', 'field_source_title_value')->description('Job title');
    $this->addFieldMapping('field_bio', 'body')->description('Bio');

    // By default, the primary key is the uid from the users table,
    // but since our user source is a source nid, need to replace
    // uid with the nid of the source node.
    $this->map = new MigrateSQLMap($this->machineName,
      array(
        'nid' => array('type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'Source node nid',
          'alias' => 'n',
        ),
      ),
      DrupalDestinationUser::getKeySchema()
    );
  }

  /**
   * Override of DrupalUser6Migration::query()
   *
   * @return mixed
   */
  protected function query() {
    // Create query from scratch to get source nodes referenced from field_source
    $query = Database::getConnection('default', $this->sourceConnection)
      ->select('node', 'n')
      ->fields('n', array('nid', 'title'));
    $query->innerJoin('content_field_source', 'cfs', 'cfs.field_source_nid = n.nid');
    $query->innerJoin('content_type_source', 'cts', 'cts.nid = cfs.field_source_nid');
    $query->fields('cts', array('field_source_title_value', 'field_twitter_id_value'));
    $query->innerJoin('node_revisions', 'nr', 'n.nid = nr.nid');
    $query->fields('nr', array('body'));
    $query->distinct();

    return $query;
  }
}

So my query works fine and gets all the relevant Source nodes, but when I ran it the first time, I realized I had to change my source PK to be the nid of the Source node instead of the default uid from the users table. As you can see above, I overrode the map with a new MigrateSQLMap object in my constructor. However, when I run the migration, it creates the first user from the query, but then all of the subsequent source records also point to that first new user instead of creating new users. Now that I've defined a new source PK, do I also need to do something on the destination side to cause it to create new users?

Thanks.

  • You might find it easier to export (and therefore abstractify) the source users (nodes) to an xml file or files and import them through feeds. – Darvanen Nov 27 '14 at 2:23
  • But why add that whole layer of work if (as I'm suspecting it is) all I need is a small tweak to my migration? – wonder95 Nov 28 '14 at 4:35
  • I agree it's not the best solution, simply an alternative if the tweak is not forthcoming. Don't know migrate so I can't really help there. – Darvanen Nov 28 '14 at 10:28
0

Since you know SQL, It would be simpler and definitely quicker to use SQL and SELECT the users from the source nodes and INSERT it into the user table rather than attempting to write a PHP script.

  • It sounds like you're not familiar with either the migrate or migrate_d2d modules. The whole idea is that it handles the migration from one Drupal site to another. Just writing a script isn't a good way to go at all, because it just bypasses the Drupal API, which could lead to problems down the road. The issue I'm having here is that there seems to be an issue in the mapping of sources in the source site to the new users in the destination site. – wonder95 Nov 28 '14 at 4:34
  • Yes you are right I don't use the migrate module. – sridhar pandurangiah Nov 28 '14 at 4:41
0

It appears that my problem was that I didn't have an orderBy in my query. Once I changed it so that it looks like:

$query = Database::getConnection('default', $this->sourceConnection)
  ->select('node', 'n')
  ->fields('n', array('nid', 'title'));
$query->innerJoin('content_field_source', 'cfs', 'cfs.field_source_nid = n.nid');
$query->innerJoin('content_type_source', 'cts', 'cts.nid = cfs.field_source_nid');
$query->fields('cts', array('field_source_title_value', 'field_twitter_id_value'));
$query->innerJoin('node_revisions', 'nr', 'n.nid = nr.nid');
$query->fields('nr', array('body'));
$query->orderBy('nid', 'asc');
$query->distinct();

the problem went away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.