2
votes

I`ve been trying to setup my migrate module for a while without any success to associate taxonomy terms to node.

Lets first start by saying, its not D2D migration, source is old symfony database!

First I`ve exported taxonomy terms successfuly:

*-.migrate.inc:

...
  'symfony_db_countries' => array(
    'class_name' => 'MigrateSymfonyCountriesMigration',
    'group_name' => 'symfony_db_loc',
  ),
  'symfony_db_cities' => array(
    'class_name' => 'MigrateSymfonyCitiesMigration',
    'group_name' => 'symfony_db_loc',
    'dependencies' => array('symfony_db_countries'),
  ),    
  'symfony_db_sights' => array(
    'class_name' => 'MigrateSymfonySightsMigration',
    'group_name' => 'symfony_db_loc',
  ),
...

Cities.inc:

class MigrateSymfonyCitiesMigration extends MigrateSymfonyCitiesMigrationBase {

  const TABLE_NAME = 'ya_loc_city';

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

    $this->description = t('Migrate cities from Symfony DB.');

    $query = Database::getConnection('symfony', 'default')->select('ya_loc_city', 'c');
    $query->leftJoin('ya_loc_city_translation', 'ct', 'ct.id = c.id');
    $query->fields('c', array());
    $query->fields('ct', array());

    $this->source = new MigrateSourceSQL($query, array(), NULL, array('map_joinable' => FALSE));

    $this->destination = new MigrateDestinationTerm('cities');

    $this->map = new MigrateSQLMap($this->machineName,
        array(
          'id' => array(
            'type' => 'int',
            'not null' => TRUE,
            'description' => 'City ID',
          ),
        ),
        MigrateDestinationTerm::getKeySchema()
    );

    $this->addFieldMapping('name', 'name');
    $this->addFieldMapping('parent', 'country_id')->sourceMigration('symfony_db_countries');
  }
} 

Then Im creating nodes from another table, which have a entityReference field (field_city) to the taxonomy term.

<?php
class MigrateSymfonySightsMigration extends Migration {
  public function prepareRow($row) {
    parent::prepareRow($row);
    $row->point = 'point (' . $row->longitude . ' ' . $row->latitude. ')';
  }

  public function __construct($arguments = array()) {
    //entry is the name of a group.
    parent::__construct($arguments);
    $this->description = 'Migration sights';

    /*************SOURCE DATA*************/
    ... 

    $this->source = new MigrateSourceSQL($query, array(), NULL,
      array('map_joinable' => FALSE));

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

    /*************MAPPING*************/

    $this->addFieldMapping('field_city', 'name')->sourceMigration('symfony_db_cities');

    $this->map = new MigrateSQLMap($this->machineName,
       array(
         'id' => array('type' => 'int',
                            'unsigned' => TRUE,
                            'not null' => TRUE,
                      )
       ),
       MigrateDestinationNode::getKeySchema()
    );
  }
}

From symfony table there is a column "name", which contains the city name like "Paris".

How to associate this column ('name') value ('Paris') with taxonomy term? Following doesnt work:

$this->addFieldMapping('field_city', 'name')->sourceMigration('symfony_db_cities');
1

1 Answers

0
votes

Firstly, make sure your "field_city" field accepts "cities" terms.

Then make sure MigrateSymfonyCitiesMigration is a dependency of MigrateSymfonySightsMigration.

According to the documentation, the term reference field migration has a "source_type" of term name by default. Make sure "ignore_case" is set appropriately.

If all else fails, then change the MigrateSymfonyCitiesMigration map sourceid1 to the "name" instead of "id" and try again (will have to reregister migration):

$this->map = new MigrateSQLMap($this->machineName,
    array(
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'City Name',
      ),
    ),
    MigrateDestinationTerm::getKeySchema()
);