I'm trying to migrate nodes with a term reference field. The terms are not migrated and already exist in the destination site. There is a method in the migration class which is confirmed to already select the correct term ID on the new site.
When the migration is run an error appears:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'migration_db.taxonomy_index' doesn't exist: [error]
SELECT 1 AS expression
FROM
{taxonomy_index} taxonomy_index
WHERE ( (nid = :db_condition_placeholder_0) AND (tid = :db_condition_placeholder_1) AND (status =
:db_condition_placeholder_2) ); Array
(
[:db_condition_placeholder_0] => 2290
[:db_condition_placeholder_1] => 4
[:db_condition_placeholder_2] => 1
)
(/home/vagrant/docroot/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:770)
The migration appears to be trying to get the term data from the migration database instead of the destination (default) database.
migrate plus yml:
...
process:
field_section: Section
...
migration class:
class MyNodeMigration extends SqlBase {
public function query() {
return $this->select('TableA', 'ta')
->fields('ta', ['Id', 'Body', 'OriginalId']);
}
...
public function prepareRow(Row $row) {
$row->setSourceProperty('Section', $this->convertToTermId($row->getSourceProperty('OriginalId'));
}
public function convertToTermId($original_id) {
return $this->getDatabase()->select('TableB', 'tb')
->fields('tb', ['DrupalTermId'])
->condition('OldId', $original_id)
->execute()
->fetchField();
}
}
-------------UPDATE-------------
I've also tried using a custom plugin. The same error occurs. Plugin class:
/**
*
* @MigrateProcessPlugin(
* id = "existing_term"
* )
*/
class MigrateProcessExistingTerm extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$term = Term::load($value);
return [
'target_id' => $term->id(),
];
}
}
Updated yml:
...
process:
field_section:
plugin: existing_term
source: Section
...