I am trying to create a hierarchical taxonomy from two different tables in the source database, one containing the parent entries, and the other containing the children. Here is my query:

    $query1 = $this->getConnection()
      ->select('envirolib_area', 'a')
      ->fields('a', array('areaname', 'areaname_japanese', 'sort_order', 'acronym', 'areaid'));
    $query2 = $this->getConnection()
      ->select('envirolib_task', 't')
      ->fields('t', array('taskname','taskname_japanese','sort_order','acronym','taskid'));
    $query1->union($query2, 'UNION ALL');

    $queryx = $this->getConnection()
      ->select($query1,'x')->fields('x', array('areaname','areaname_japanese', 'sort_order', 'acronym', 'areaid', 'parentid'));

The envirolib_area table has a primary key of areaid, and the envirolib_task table has a primary key of taskid. The envirolib_area table doesn't have a taskid column, and in my union I am actually calling a.areaid and t.taskid areaid, with t.areaid called parentid and 0 inserted in that column for rows from the envirolib_area table.

the addition of $queryx was necessary to stave of errors about unven numbers of queries in a union, which seems to be caused by the mapping method joining onto the query (thought the explanation I read, claimed that shouldn't happen if 'map_joinable' => FALSE as it is in my case.

So here is my attempt at MigrateSQLMap:

$this->map = new MigrateSQLMap($this->machineName,
    'areaid' => array('type' => 'int',
                       'length' => 3,
                       'not null' => TRUE,
                       'description' => 'Area ID',
                       'alias' => 'a'
    'taskid' => array('type' => 'int',
                        'length' => 3,
                        'not null' => TRUE,
                        'description' => 'Parent ID',
                        'alias' => 't')

When I try to run the migration I get a wall of errors like this:

Undefined property: stdClass::$taskid File /Users/bob/ultrabob.prv/www/sites/all/modules/migrate/includes/migration.inc, line 943(file: /Users/bob/ultrabob.prv/www/sites/all/modules/migrate/includes/migration.inc, line 943)
Could not save to map table due to NULL value for key field taskid

How should I set up the MigrateSQLMap in a case like this? Would it make more sense to migrate this taxonomy in two migrations, one for each table?


Either do 2 migrations. Or create a derivative table of your term data to easily set the $query for the migration before running the migration task.

You can do this by writing a custom php script to be called by drush php-script or by using the preImport migrate hook to build/rebuild the derivative table.

| improve this answer | |
  • Is there any means of chaining two migrations -- automatically running the second upon completion of the first? – UltraBob Oct 5 '15 at 22:33
  • 1
    you can define migration dependencies in your hook_migrate function. Therefore enforcing 1 completes the other. You can define a migration group in hook_migrate so that you can simply run drush mi --group=MYGROUP and it will run all migrations in that group, in the dependent order. – tenken Oct 6 '15 at 1:25

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.