I am trying to import nodes of type operation and ports from xml, operations have a reference entity field that refers to ports. Somehow the reference is not correct, here is my xml

operations.xml

<?xml version="1.0" encoding="UTF-8"?>
<content>
  <node>
    <nid>17</nid>
    <title>A1</title>
    <description/>
    <classification>Category A</classification>
    <portnid>15</portnid>
  </node>
  <node>
    <nid>22</nid>
    <title>A2</title>
    <description>A2 description</description>
    <classification>Category A</classification>
    <portnid>21</portnid>

  </node>
</content>

ports.xml

<?xml version="1.0" encoding="UTF-8"?>
<content>
  <node>
    <nid>15</nid>
    <title>port1</title>
    <port_type>1</port_type>
  </node>
  <node>
    <nid>21</nid>
    <title>port2</title>
    <port_type>2</port_type>
  </node>
</content>

port mappings

    $this->addFieldMapping('title', 'title')
        ->xpath('title');
    $this->addFieldMapping('field_xd_port_type', 'port_type')
        ->xpath('port_type');

operation mappings

    $this->addFieldMapping('title', 'title')
        ->xpath('title');
    $this->addFieldMapping('field_xd_classification', 'classification')
        ->xpath('classification');
    $this->addFieldMapping('field_xd_operation_description', 'description')
        ->xpath('description');
    $this->addFieldMapping('field_xd_ports', 'portnid')
        ->sourceMigration('importPorts');

There is no error but the ports are not correctly referenced either.

Comments

chantal.rosmuller created an issue. See original summary.

chantal.rosmuller’s picture

Here is the complete code

class ImportPortsMigration extends XMLMigration {

  public function __construct($arguments) {
    parent::__construct($arguments);

    $this->description = t('Migrate ports');

    $xml_folder = DRUPAL_ROOT . '/' . drupal_get_path('module', '_import') . '/xml/';
    $items_url = $xml_folder . 'ports.xml';

    $fields = array(
      'nid' => t('Port Node ID'),
      'title' => t('Title'),
      'port_type' => t('Port Type'),
    );

    $item_xpath = '/content/node';
    $item_ID_xpath = 'nid';
    $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_ID_xpath);



    $this->source = new MigrateSourceMultiItems($items_class, $fields);

    $this->destination = new MigrateDestinationNode('experd_port');

    $this->map = new MigrateSQLMap($this->machineName, array(
      'nid' => array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => TRUE,
      )
        ), MigrateDestinationNode::getKeySchema()
    );

    $this->addFieldMapping('title', 'title')
        ->xpath('title');
    $this->addFieldMapping('field_xd_port_type', 'port_type')
        ->xpath('port_type');
  }

}

class ImportOperationsMigration extends XMLMigration {

  public function __construct($arguments) {
    parent::__construct($arguments);

    $this->description = t('Migrate operations');

    $xml_folder = DRUPAL_ROOT . '/' . drupal_get_path('module', '_import') . '/xml/';
    $items_url = $xml_folder . 'operations.xml';

    $fields = array(
      'title' => t('Title'),
      'classification' => t('Classification'),
      'description' => t('Description'),
      'portnid' => t('Ports')
    );

    $item_xpath = '/content/node';
    $item_ID_xpath = 'nid';
    $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_ID_xpath);



    $this->source = new MigrateSourceMultiItems($items_class, $fields);

    $this->destination = new MigrateDestinationNode('experd_operation');

    $this->map = new MigrateSQLMap($this->machineName, array(
      'nid' => array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => TRUE,
      )
        ), MigrateDestinationNode::getKeySchema()
    );

    $this->addFieldMapping('title', 'title')
        ->xpath('title');
    $this->addFieldMapping('field_xd_classification', 'classification')
        ->xpath('classification');
    $this->addFieldMapping('field_xd_operation_description', 'description')
        ->xpath('description');

 

    
    $this->addFieldMapping('field_xd_ports', 'portnid')
         ->sourceMigration('importPorts');

    $this->addFieldMapping('field_xd_courses:create_term')
         ->defaultValue(TRUE);
  }

}
chantal.rosmuller’s picture

I fixed it my sqlf,

I changed the ports xml:

<?xml version="1.0" encoding="UTF-8"?>

15 port1 1
21 port2 2

and I changed the sourcemigration name

$this->addFieldMapping('field_xd_ports', 'portnid')
->sourceMigration('defaultports');

Adding drush_print_r to preparerow and prepare and running the migration with drush gave me some clarity about wat was going wrong.

chantal.rosmuller’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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